1

I've been using Unity for several years now...and I've never come across this issue.

Whenever I try to apply or revert a prefab, it is giving the following error:

NullReferenceException: Object reference not set to an instance of an object UnityEditor.PrefabUtility.RegisterNewObjects (System.Collections.Generic.List1 newHierarchy, System.Collections.Generic.List1 hierarchy, System.String actionName) (at C:/buildslave/unity/build/Editor/Mono/ImportSettings/PrefabUtility.cs:121)

(Full Stack trace below)

Any idea what is the cause? The changes do seem to apply, I've moved things around in the tree, and modified values and they do apply. But the error is somewhat concerning and I'd rather not have an error. Any ideas/suggestions?

Full Stack Trace

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)

EDIT: I was asked to explain how its different to the flagged duplicate question. This is not asking simply "What is a Null Reference". I'm fully aware of a null reference (as I said I've used Unity for many years now). This question is in regards to a specific case of null references, where applying a Unity prefab is causing a null reference in the editor, referencing UnityEditor scripts (that I haven't written).

NeoKuro
  • 457
  • 1
  • 5
  • 21
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – SᴇM May 05 '18 at 12:37
  • The log shows that the error is not coming from your script. Restart Unity. If restarting doesn't solve this then update or downgrade your Unity version since it is likely a bug. – Programmer May 05 '18 at 12:52
  • 3
    This is clearly not a duplicate of `What is a NullReferenceException`. Maybe this link could help you: https://answers.unity.com/questions/1461526/null-ref-exception-in-unity-backend.html – Basile Perrenoud May 05 '18 at 18:46
  • 1
    Yeah I know what a Null Reference is, but I've never come across this case of a null error. I restarted unity and the problem persisted, however my co-workers don't get this error when they apply that prefab (I believe) so I don't know if something broke in my version (in which case a total re-install may be needed?) I did try looking through and finding a missing script in the answer suggested by @BasilePerrenoud but I couldn't find one...course the prefab is very large many many child objects exist within it so its entirely possible I missed it – NeoKuro May 06 '18 at 13:54
  • As it seems to be internal Unity error you have better chance looking at Unity forums or opening ticket for error and ask developer to fix. (If @Programmer hints does not help) – R2RT May 06 '18 at 14:14
  • I thought as much. I just don't tend to get many responses at Unity so thought I'd try here first haha. – NeoKuro May 08 '18 at 12:58
  • I had a prefab that was throwing this error too. My bad though. I had unchecked it in the Inspector for a previous debug scenario and saved it back. The Apply button is dangerous. In the Hierarchy it shows greyed labels but in the project it is blue. I assumed all was well when it was not. I checked it resaved it and now error has subsided. Whew. I was seeing too many Unity system bug reports about this and cringed at updating Unity in the middle of a project. I hope this helps. – Paul Moore Jan 26 '19 at 22:37

2 Answers2

0

I had the same problem, and yes I was like "I know what is a NullReferenceException", but why it's happening here? Well, I can tell you why but don't how to fix, but I have a work around.

Why: Unity has an editor tool for prefabs, so you have that button to Apply and it will update the prefab, the problem is the scrip that Unity is running is not finding the path/id to get you base prefab to update it, and yes I tried to delete and create again, I tried to reopen the project etc and Unity still not finding the prefab id. A quick observation, I deleted the original prefab then I created a new prefab, I deleted all instances on the game leaving then only the brand new prefab and when I created a new instance of this prefab... the error still there, I can't update, so it's something internal on Unity and there's nothing we can do about it.

Workaround: The good news is that we have a workaround for that, drag the instance from the Hierarchy to the Project, hover on the top of your prefab until the blue square appears around it, then release your object and after confirm you want to replace your prefab will be updated.

And finally, Unity is updating the prefab system, and everybody celebrated on Unite 2018 like our team just won the final of a world cup. I believe soon this problem will be only a joke, fingers crossed.

Fernando Bonet
  • 576
  • 5
  • 13
  • 1
    Simply replacing the prefab with a drag-n-drop from hierarchy did NOT fix this for me. – murgo Aug 03 '18 at 16:52
  • I'm sorry to hear that, seems you have a even bigger problem. I had a problem with Unity changing my monitor brightness to almost zero (seriously) and I tried everything to solve, the solution I found, uninstall and install a different version :P it solved. And as I said even they identify this as a problem they will never put efforts to solve because the new prefab system is about to be released, a bittersweet thing for you. – Fernando Bonet Aug 06 '18 at 09:54
0

This problem is due to a "Missing Reference Script" in your prefab. Somewhere in its hierarchy, you have a Missing Script or a Missing Prefab (in your drag&drops elements, probably).

I've deleted the components with "Missing Reference Script" and set to "None" drag&drop elements in my inspector, reapply, and it worked !

Rewar
  • 74
  • 6