If you’re like me you use lots of custom inspectors and/or windows in Unity. It helps keep things organized and easy to use. However every once in a while you come across some weird ‘bug’ that makes you want to do things that are not for the betterment of humankind.
Today’s issue is getting a missing reference error that looks something like this.
- MissingReferenceException: The object of type ‘ValveAnim’ has been destroyed but you are still trying to access it.
- Your script should either check if it is null or you should not destroy the object.
- UnityEditor.Editor.IsEnabled () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:589)
- UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/ buildslave/unity/build/ Editor/Mono/Inspector/InspectorWindow.cs:1151)
- UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor editors) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1028)
- UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:352)
- 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)
This error occured when I tried to replace a gameobject (prefab instance) through a custom inspector from one of its components. This inspector would show the prefabs that are compatible with the input data, which you can then select and replace the existing gameobject.
This is done simply by creating an instance of the selected prefab, copying over some small amount of data from the original, and then destroying the original. Seems simple right?
This might make you wonder, where exactly did you do something wrong? You’d check to see if any of your inspectors or windows are trying to access that missing component and as you check things it seems that you’re doing everything okay. It turns out it is Unity’s own inspectors/windows that are trying to render the inspector but fail to find the components and then they proceed to tell you “Your script should either check if it is null or you should not destroy the object.” Blaming you, but as the error shows it’s coming from inside Unity itself. It’s in the fracking ship… err… Editor.
In our case the gameobject we destroy has more components next to the SensorPrefab, the next in line is the ValveAnim, which is mentioned in our error message. So, you’d say, why doesn’t Unity just stop drawing the inspector tab if the selected gameobject gets destroyed? I have no idea but we have a handy little function that tells it to do just that.
This will tell it to stop what the hell it’s doing and just go with the flow, man! Now you can destroy your own gameobjects from their own inspectors without those pesky little error messages.
Thanks to margutteter for stopping me from killing myself. (http://answers.unity3d.com/answers/953420/view.html)