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)

An idea is to disable it instead of destroy?
Destroy function is very CPU costly and if youll reinstantiate it later - itll 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 its a meshfiller or meshcollider. If you really want to destroy it - first destroy or disable those:) But again id 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 :slight_smile: )

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.