我已经使用Unity好几年了......而且我从未遇到过这个问题。
每当我尝试应用或还原预制件时,都会出现以下错误:
NullReferenceException:未将对象引用设置为对象的实例
UnityEditor.PrefabUtility.RegisterNewObjects(System.Collections.Generic.List 1 newHierarchy, System.Collections.Generic.List
1层次结构,System.String actionName)(在C:/buildslave/unity/build/Editor/Mono/ImportSettings/PrefabUtility.cs:121)< / p>
(下面的完整堆栈跟踪)
任何想法是什么原因?这些变化确实似乎适用,我已经在树中移动了一些东西,并修改了值,它们确实适用。但错误有点令人担忧,我宁愿没有错误。有什么想法/建议吗?
完整堆栈跟踪
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.PrefabUtility.RegisterNewObjects (System.Collections.Generic.List`1 newHierarchy, System.Collections.Generic.List`1 hierarchy, System.String actionName) (at C:/buildslave/unity/build/Editor/Mono/ImportSettings/PrefabUtility.cs:121)
UnityEditor.PrefabUtility.ReplacePrefabWithUndo (UnityEngine.GameObject target) (at C:/buildslave/unity/build/Editor/Mono/ImportSettings/PrefabUtility.cs:223)
UnityEditor.GameObjectInspector.DoPrefabButtons (PrefabType prefabType, UnityEngine.GameObject go) (at C:/buildslave/unity/build/Editor/Mono/Inspector/GameObjectInspector.cs:347)
UnityEditor.GameObjectInspector.DrawInspector () (at C:/buildslave/unity/build/Editor/Mono/Inspector/GameObjectInspector.cs:260)
UnityEditor.GameObjectInspector.OnHeaderGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/GameObjectInspector.cs:170)
UnityEditor.Editor.DrawHeader () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:420)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor[] editors, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1125)
UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1030)
UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:361)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:295)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:288)
UnityEditor.HostView.InvokeOnGUI (Rect onGUIPosition) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:255)
编辑:我被要求解释它与标记的重复问题的不同之处。这并不是简单地问“什么是空参考”。我完全知道一个空引用(正如我所说,我已经使用Unity多年了)。这个问题涉及空引用的特定情况,其中应用Unity预制件会在编辑器中引起空引用,引用UnityEditor脚本(我还没有写过)。
答案 0 :(得分:1)
此问题是由于预制件中的“缺少参考脚本”引起的。在其层次结构中的某个位置,您有一个脚本缺失或预制件缺失(可能在拖放元素中)。
我已使用“缺少参考脚本”删除了这些组件,并在检查器中将其设置为“无”拖放元素,重新应用,它就可以了!
答案 1 :(得分:0)
我遇到了同样的问题,是的,我就像“我知道什么是NullReferenceException”一样,但是为什么在这里发生呢?好吧,我可以告诉您原因,但不解决该问题,但是我已经解决了。
为什么:Unity有一个用于预制件的编辑器工具,所以您具有要应用的按钮,它将更新预制件,问题是Unity正在运行的脚本无法找到要更新的基础预制件的路径/ id它,是的,我试图删除并再次创建,我试图重新打开项目等,而Unity仍未找到预制ID。快速观察,我删除了原始预制件,然后创建了一个新的预制件,删除了游戏中的所有实例,只剩下了一个全新的预制件,当我创建了这个预制件的新实例时……错误仍然存在,我可以不会更新,所以这是Unity的内部功能,我们对此无能为力。
解决方法:好消息是,我们有一个解决方法,将实例从“层次结构”拖动到“项目”,将鼠标悬停在预制件的顶部,直到其周围出现蓝色方框,然后释放对象,并在确认您之后想要替换的预制件将被更新。
最后,Unity正在更新预制系统,像我们的团队一样,每个人都在Unity 2018上庆祝了世界杯的决赛。我相信很快这个问题将只是一个玩笑,两手交叉。