目录
- 导出游戏时需要选择空的文件夹
- CS0103错误:使用的变量名或者方法名并不存在于当前上下文中
- CS1061错误: 尝试调用方法或访问不存在的类成员
- Unity怎么点都没反应:可能是进入了死循环
- CS0428错误: 类型转换错误
- CS1656错误: 无效赋值错误
- NullReferenceException:空指针错误,Object并没有作为一个对象的实例
- UnassignedReferenceException: 变量未被赋值错误
- 场景不能加载问题
- IndexOutOfRangeException: 数组下标越界问题
- ArgumentException: 要实例化的对象是空的
- ArgumentOutOfRangeException: 超出索引
- CS0108警告: 变量声明与基类有相同的名字
- InvalidCastException: 强制转换异常
- FormatException: 输入的字符串格式有误
- StackOverflowException: 请求的操作导致堆栈溢出
- InvalidOperationException: 输入系统包设置问题
- 在场景中有多个音频监听器
- 父物体设置不成功
- Component GUI Layer in Main Camera for Scene Test.unity is no longer available:不再支持场景Test.unity中挂载在Main Camera上的组件GUI Layer
- The namespace 'global namespace' already contains a definition for 'Test':项目资源中存在和Test重复名称的脚本被调用
- PrecompiledAssemblyException: 在当前平台包含多个同名 Test.dll 的预编译程序集,每个平台只允许一个具有相同名称的程序集
- CS0656错误: 缺少编译器要求的成员“Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create”
- 无法正确读取资源的GUID
- Visual Scripting版本不兼容问题
- Package Manager:A warning occurred: You do not have entitlements for this package.
- 编辑器布局无法完全加载问题
- hierarchy栏目中的gameobject不能拖动到prefab上的脚本中
- 没有显示在Console中的编译错误可能问题
导出游戏时需要选择空的文件夹
否则会出现以下错误:
CS0103错误:使用的变量名或者方法名并不存在于当前上下文中
错误: error CS0103: The name ‘gameobject’ does not exist in the current context
解决: 脚本中获取游戏对象使用gameObject。
注:Debug.Log(“”);表示调试时输出引号中的内容。
CS1061错误: 尝试调用方法或访问不存在的类成员
错误: error CS1061: ‘Vector2’ does not contain a definition for ‘X’ and no accessible extension method ‘X’ accepting a first argument of type ‘Vector2’ could be found (are you missing a using directive or an assembly reference?)
解决: 脚本中获取游戏对象的位置使用x、y、z。
①请确保正确输入了成员名。
②如果有权修改此类,则可以添加缺少的成员并实现它。
③如果没有修改此类的权限,则可以添加扩展方法。
Unity怎么点都没反应:可能是进入了死循环
问题: Unity怎么点击都没反应,但又不是卡住了,考虑可能是代码写的不当,出现了死循环代码。
解决: 先用任务管理器将Unity关闭,再仔细检查代码,确定逻辑无误后再调试运行。
CS0428错误: 类型转换错误
检查代码或者使用强制转换。
CS1656错误: 无效赋值错误
检查代码的赋值是否正确。
NullReferenceException:空指针错误,Object并没有作为一个对象的实例
错误:NullReferenceException: Object reference not set to an instance of an object
解决:对象需要实例化后才能使用。
深刻的教训:有次为了图方便,对象名是复制的,多复制了一个空格,导致一直报没有实例化的错误,找了两天才找到。இ௰இ
UnassignedReferenceException: 变量未被赋值错误
错误:UnassignedReferenceException: The variable XXXX of XXXXX has not been assigned.
解决:脚本中写明该变量为public,而在Inspector窗口中却未给其赋值,在Inspector窗口赋值即可。
场景不能加载问题
错误:Scene ‘WhacAMole’ couldn’t be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings…
解决:选择 File->Build Settings->Add open scenes
IndexOutOfRangeException: 数组下标越界问题
错误:IndexOutOfRangeException: Index was outside the bounds of the array.
解决:限制数组索引,使其不要越界。
ArgumentException: 要实例化的对象是空的
错误:ArgumentException: The Object you want to instantiate is null.
解决:在Inspector或Scripts中引用要实例化的对象。
ArgumentOutOfRangeException: 超出索引
错误:ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
解决:注意访问集合元素时,索引是否超出了范围。
CS0108警告: 变量声明与基类有相同的名字
错误:warning CS0108: ‘MouseDrag.camera’ hides inherited member ‘Component.camera’. Use the new keyword if hiding was intended.
解决一:在变量声明处添加new关键字。
解决二:改变变量的名字避免产生混淆。
InvalidCastException: 强制转换异常
错误:InvalidCastException: Specified cast is not valid.
解决:Unity中AudioSource使用Resources.Load()方法加载。
FormatException: 输入的字符串格式有误
错误:FormatException: Input string was not in a correct format.
解决:检查读取到的值的格式。
StackOverflowException: 请求的操作导致堆栈溢出
错误:StackOverflowException: The requested operation caused a stack overflow.
解决:检查程序中是否存在递归调用,导致程序卡死。
InvalidOperationException: 输入系统包设置问题
错误:InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.
解决:点击菜单栏的Edit->Project Settings…->Player->Other settings,将active lnput handling选项设置为Both即可。
在场景中有多个音频监听器
提示:There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
解决:一般新创建一个场景后,在Main Camera中都有一个Audio Listener组件,用于监听场景中是否有音频的播放。如果需要场景中的音乐不随场景的切换而重置,需要场景中只有一个Audio Listener,删除一个即可。
父物体设置不成功
错误:Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: ‘AudioCube’).
//加载预制体
GameObject audioCube = Resources.Load<GameObject>("AudioCube");
//初始化预制体
GameObject gameObject = Instantiate(audioCube, new Vector3(1, 0, 0), Quaternion.identity);
//设置父物体
audioCube.transform.SetParent(AudioCubes.transform);
预想将使用预制体audioCube实例化的物体的父物体设置为AudioCubes,如上方法会导致设置不成功。
解决:
使用Instantiate的重载方法直接把物体的父物体设置好就不会有如上问题:
//加载预制体
GameObject audioCube = Resources.Load<GameObject>("AudioCube");
//初始化预制体
GameObject gameObject = Instantiate(audioCube, new Vector3(1, 0, 0), Quaternion.identity,AudioCubes.transform);
Component GUI Layer in Main Camera for Scene Test.unity is no longer available:不再支持场景Test.unity中挂载在Main Camera上的组件GUI Layer
错误:Component GUI Layer in Main Camera for Scene Test.unity is no longer available.
解决:将Main Camera上的Flare Layer组件移除即可。
The namespace ‘global namespace’ already contains a definition for ‘Test’:项目资源中存在和Test重复名称的脚本被调用
错误:The namespace ‘global namespace’ already contains a definition for ‘Test’.
解决:找到相同名字的脚本,修改其中一个即可。
PrecompiledAssemblyException: 在当前平台包含多个同名 Test.dll 的预编译程序集,每个平台只允许一个具有相同名称的程序集
错误:PrecompiledAssemblyException: Multiple precompiled assemblies with the same name Test.dll included or the current platform. Only one assembly with the same name is allowed per platform.
解决:找到重复的另一个Test.dll,删除掉即可。
CS0656错误: 缺少编译器要求的成员“Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create”
错误:error CS0656:Missing compiler required member ‘Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create’.
原因:在net core使用动态类型dynamic。
解决:
1.不用dynamic类型。
2.在使用的地方添加一个dll,Microsoft.CSharp,或者用NuGet添加Microsoft.CSharp即可。
在Visual Studio中打开NuGet包管理器,搜索Microsoft.CSharp后点击安装。
无法正确读取资源的GUID
错误:Could not extract GUID in text file XXX at line XXX.
解决:重置项目资源的GUID
- 菜单栏->Edit->ProjectSettings->左侧Editor菜单栏,右侧找到Asset Serialization,将其Mode设置为Force Text:
- 在Project面板的Assets目录下新建一个名为Editor的目录,在其中新建一个UnityGuidRegeneratorMenu脚本,写入如下代码,注意其中的路径需要修改成自己需要重置GUID的文件夹目录
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
namespace UnityGuidRegenerator {
public class UnityGuidRegeneratorMenu {
[MenuItem("Tools/Regenerate asset GUIDs")]
public static void RegenerateGuids() {
if (EditorUtility.DisplayDialog("GUIDs regeneration",
"You are going to start the process of GUID regeneration. This may have unexpected results. \n\n MAKE A PROJECT BACKUP BEFORE PROCEEDING!",
"Regenerate GUIDs", "Cancel")) {
try {
AssetDatabase.StartAssetEditing();
string path = Path.GetFullPath(".") + Path.DirectorySeparatorChar + "Assets/_Game";//要更新guid的文件夹
UnityGuidRegenerator regenerator = new UnityGuidRegenerator(path);
regenerator.RegenerateGuids();
}
finally {
AssetDatabase.StopAssetEditing();
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
}
}
}
}
internal class UnityGuidRegenerator {
private static readonly string[] kDefaultFileExtensions = {
// "*.meta",
// "*.mat",
// "*.anim",
// "*.prefab",
// "*.unity",
// "*.asset"
"*.*"
};
private readonly string _assetsPath;
public UnityGuidRegenerator(string assetsPath) {
_assetsPath = assetsPath;
}
public void RegenerateGuids(string[] regeneratedExtensions = null) {
if (regeneratedExtensions == null) {
regeneratedExtensions = kDefaultFileExtensions;
}
// Get list of working files
List<string> filesPaths = new List<string>();
foreach (string extension in regeneratedExtensions) {
filesPaths.AddRange(
Directory.GetFiles(_assetsPath, extension, SearchOption.AllDirectories)
);
}
// Create dictionary to hold old-to-new GUID map
Dictionary<string, string> guidOldToNewMap = new Dictionary<string, string>();
Dictionary<string, List<string>> guidsInFileMap = new Dictionary<string, List<string>>();
// We must only replace GUIDs for Resources present in Assets.
// Otherwise built-in resources (shader, meshes etc) get overwritten.
HashSet<string> ownGuids = new HashSet<string>();
// Traverse all files, remember which GUIDs are in which files and generate new GUIDs
int counter = 0;
foreach (string filePath in filesPaths) {
EditorUtility.DisplayProgressBar("Scanning Assets folder", MakeRelativePath(_assetsPath, filePath), counter / (float) filesPaths.Count);
string contents = File.ReadAllText(filePath);
IEnumerable<string> guids = GetGuids(contents);
bool isFirstGuid = true;
foreach (string oldGuid in guids) {
// First GUID in .meta file is always the GUID of the asset itself
if (isFirstGuid && Path.GetExtension(filePath) == ".meta") {
ownGuids.Add(oldGuid);
isFirstGuid = false;
}
// Generate and save new GUID if we haven't added it before
if (!guidOldToNewMap.ContainsKey(oldGuid)) {
string newGuid = Guid.NewGuid().ToString("N");
guidOldToNewMap.Add(oldGuid, newGuid);
}
if (!guidsInFileMap.ContainsKey(filePath))
guidsInFileMap[filePath] = new List<string>();
if (!guidsInFileMap[filePath].Contains(oldGuid)) {
guidsInFileMap[filePath].Add(oldGuid);
}
}
counter++;
}
// Traverse the files again and replace the old GUIDs
counter = -1;
int guidsInFileMapKeysCount = guidsInFileMap.Keys.Count;
foreach (string filePath in guidsInFileMap.Keys) {
EditorUtility.DisplayProgressBar("Regenerating GUIDs", MakeRelativePath(_assetsPath, filePath), counter / (float) guidsInFileMapKeysCount);
counter++;
string contents = File.ReadAllText(filePath);
foreach (string oldGuid in guidsInFileMap[filePath]) {
if (!ownGuids.Contains(oldGuid))
continue;
string newGuid = guidOldToNewMap[oldGuid];
if (string.IsNullOrEmpty(newGuid))
throw new NullReferenceException("newGuid == null");
contents = contents.Replace("guid: " + oldGuid, "guid: " + newGuid);
}
File.WriteAllText(filePath, contents);
}
EditorUtility.ClearProgressBar();
}
private static IEnumerable<string> GetGuids(string text) {
const string guidStart = "guid: ";
const int guidLength = 32;
int textLength = text.Length;
int guidStartLength = guidStart.Length;
List<string> guids = new List<string>();
int index = 0;
while (index + guidStartLength + guidLength < textLength) {
index = text.IndexOf(guidStart, index, StringComparison.Ordinal);
if (index == -1)
break;
index += guidStartLength;
string guid = text.Substring(index, guidLength);
index += guidLength;
if (IsGuid(guid)) {
guids.Add(guid);
}
}
return guids;
}
private static bool IsGuid(string text) {
for (int i = 0; i < text.Length; i++) {
char c = text[i];
if (
!((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z'))
)
return false;
}
return true;
}
private static string MakeRelativePath(string fromPath, string toPath) {
Uri fromUri = new Uri(fromPath);
Uri toUri = new Uri(toPath);
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
return relativePath;
}
}
}
- 这时,返回Unity,等待编译一段时间后,可以发现菜单栏多出了一个Tool菜单,选中下面的Regenerate asset GUIDs,就可以对选定文件夹下资源的GUID进行重置了。
Visual Scripting版本不兼容问题
警告:Inspectors are disabled when plugin versions mismatch to prevent data corruption
解决:Edit > Project Settings,左侧栏找到Visual Scripting,点击Generate按钮,如下图所示:
等待Unity编译完成即可。
Package Manager:A warning occurred: You do not have entitlements for this package.
该问题是已知bug并存在于一些版本之中,可以提交bug report让官方来修复该版本的问题。
其他解决方案:
1.重启Unity Editor(一般就可以解决)
2.右键点击Assets文件夹并选择reimport all
3.删除该项目文件夹下的Library文件夹后重新打开该项目
4.升级Unity Editor版本
编辑器布局无法完全加载问题
提示:The editor layout could not be fully loaded, this can happen when the layout contains EditorWindows not available in this project
UnityEditor.WindowLayout:LoadDefaultWindowPreferences()
解决:建立的项目名称是中文名,改成英文即可。
hierarchy栏目中的gameobject不能拖动到prefab上的脚本中
问题:当把prefab从当前scene中删除之后,再添加到另外一个scene中时,unity无法找到在第一个scene中添加的gameobject。
解决:把该gameobject也制成prefab,即可以让该gameobject成为prefab的孩子。
没有显示在Console中的编译错误可能问题
- 函数引用没加()
- 用到了包,但是没有用using引用