- Home /
Inspector removing MeshRenderer generates error
If you create a custom inspector with a button that tries to removes/destroy the MeshRenderer component an exception is generated in editor/engine code that I cannot prevent.
MeshRenderer mr = obj.GetComponent<MeshRenderer>();
if (mr != null)
{
DestroyImmediate(mr);
}
It does appear to destroy the component and then no longer complain -- but I'd prefer to write code that generates no false exceptions.
Is there a way to fix this?
Paul
MissingReferenceException: The object of type 'MeshRenderer' 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-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Answer by Bunny83 · Jun 06, 2016 at 12:46 AM
You should have provided more context. However i'm sure that the problem is that the inspector window gathered the active editors at the beginning of each OnGUI call but you destroyed the Rigidbody in between. Therefore this error occures.
You should be able to use
GUIUtility.ExitGUI();
after you have destroyed the Rigidbody to terminate the current OnGUI call of the inspector. ExitGUI will raise a special exception (ExitGUIException) that is catched silently by Unity. It allows you to terminate an OnGUI call if you know that your actions will corrupt the execution of following GUI operations.
Correct, this is an Editor script so not worried about performance and really do want to destroy. There is no Rigidbody involved just $$anonymous$$eshFilter and $$anonymous$$eshRenderer components. Adding GUIUtility.ExitGUI() after the DestroyImmediate() call avoids the exception. Didn't know about the OnGUI internal behavior. Thanks.
Answer by Silvermurk · Jun 06, 2016 at 12:05 AM
An idea is to disable it instead of destroy? Destroy function is very CPU costly and if you`ll reinstantiate it later - it`ll be costly again. So first - maybe you could just disable renderer?
And as for error - this means that something on your GameObject is depentant on that renderer. I supose it`s a meshfiller or meshcollider. If you really want to destroy it - first destroy or disable those:) But again i`d say just disable instead - disabled objects don`t load cpu-gpu and they are much less costly on process time to enable-disable than to destroy and instantiate. So i strongly advice not to use destroy whenever possable (well unless you absolutly have to - like with Decals or missed laser shots in space shooters, and even those i prefer to "translate" to new position or pool instead of destroy :) )
Uhm are you sure you read the question carefully? The question was about editor scripting and not about runtime scripting.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I add something to the OnClick of a UI Button from an Editor-Script? 0 Answers
TexturePropertySingleLine in Editor class 0 Answers
How to tell if any textField has keyboard focus in editor? 2 Answers
How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers