Unity3D MissingReferenceException when removing BoxCollider
<p>I'm developing an open source editor tool for Unity3D <a href="https://github.com/JAFS6/BoxStairsTool" rel="nofollow noreferrer">https://github.com/JAFS6/BoxStairsTool</a> and I'm writting a CustomEditor.</p>

<p>I create a main GameObject and I attach my script <em>BoxStairs</em> to it. This script attachs to the same <em>GameObject</em> a <em>BoxCollider</em>.</p>

<p>On my <em>CustomEditor</em> code, I have a method which is in charge of removing both two components attached before to finalize editing.</p>

<p>This is the code:</p>

<pre><code>    private void FinalizeStairs ()
    {
        Undo.SetCurrentGroupName("Finalize stairs");
        BoxStairs script = (BoxStairs)target;
        GameObject go = script.gameObject;
        BoxCollider bc = go.GetComponent&lt;BoxCollider&gt;();

        if (bc != null)
        {
            Undo.DestroyObjectImmediate(bc);
        }
        Undo.DestroyObjectImmediate(target);
    }
</code></pre>

<p>This method is called on the method <em>OnInspectorGUI</em> after a button has been pressed</p>

<pre><code>public override void OnInspectorGUI ()
{
    ...
    if (GUILayout.Button("Finalize stairs"))
    {
        FinalizeStairs();
    }
}
</code></pre>

<p>Both two methods are on the class</p>

<pre><code>[CustomEditor(typeof(BoxStairs))]
public sealed class BoxStairsEditor : Editor
</code></pre>

<p>It actually removes the two components but, once the <em>BoxCollider</em> has been removed the following error appears:</p>

<pre><code>MissingReferenceException: The object of type 'BoxCollider' has been 
destroyed but you are still trying to access it.
</code></pre>

<p>I try to locate where the error is ocurring by looking at the trace:</p>

<pre><code>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:590)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean&amp; showImportedObjectBarNext, UnityEngine.Rect&amp; importedObjectBarRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1154)
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: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)
</code></pre>

<p>But none of my scripts appears on it.</p>

<p>I've been looking on the code where I'm referencing the <em>BoxCollider</em> and the only place is where it is created, when the stairs are created which is triggered once a change has happened on the inspector.</p>

<p>It is in the class:</p>

<pre><code>[ExecuteInEditMode]
[SelectionBase]
public sealed class BoxStairs : MonoBehaviour
</code></pre>

<p>This is the code:</p>

<pre><code>    /*
     * This method creates a disabled BoxCollider which marks the volume defined by
     * StairsWidth, StairsHeight, StairsDepth.
     */
    private void AddSelectionBox ()
    {
        BoxCollider VolumeBox = Root.GetComponent&lt;BoxCollider&gt;();

        if (VolumeBox == null)
        {
            VolumeBox = Root.AddComponent&lt;BoxCollider&gt;();
        }

        if (Pivot == PivotType.Downstairs)
        {
            VolumeBox.center = new Vector3(0, StairsHeight * 0.5f, StairsDepth * 0.5f);
        }
        else
        {
            VolumeBox.center = new Vector3(0, -StairsHeight * 0.5f, -StairsDepth * 0.5f);
        }

        VolumeBox.size = new Vector3(StairsWidth, StairsHeight, StairsDepth);

        VolumeBox.enabled = false;
    }
</code></pre>

<p>I've tried to comment this method's body to allow removing the <em>BoxCollider</em> without this "reference" and the error still appears, so, I guess this method is not the problem.</p>

<p>Also, I've removed the <em>BoxCollider</em> manually, without clicking the Finalize button to trigger this code, via right click on the component on the inspector "Remove Component" option and the error not appears and after that, click over finalize stairs and no problem shows up.</p>

<p>As @JoeBlow mentioned in the comments I've checked that the <strong>FinalizeStairs</strong> method <strong>is called just once</strong>.</p>

<p>Also I've checked that the process of creation with the call to <em>AddSelectionBox</em> method it is not happening on the moment of clicking finalize button.</p>

<p>So, please I need a hand on this. This is the link to the development branch <a href="https://github.com/JAFS6/BoxStairsTool/tree/feature/BoxStairsTool" rel="nofollow noreferrer">https://github.com/JAFS6/BoxStairsTool/tree/feature/BoxStairsTool</a>, here you will find that the above mentioned method <strong>FinalizeStairs</strong> has the code wich removes the BoxStairs script only and on that moment it throws no errors.</p>

<p>Any idea or advice on this will be very helpful. Thanks in advance.</p>

<p><strong>Edit:</strong>
a Minimal, Complete, and Verifiable example:</p>

<p><strong>Asset/BoxStairs.cs</strong></p>

<pre><code>using UnityEngine;
using System.Collections.Generic;

namespace BoxStairsTool
{
    [ExecuteInEditMode]
    [SelectionBase]
    public sealed class BoxStairs : MonoBehaviour
    {
        private GameObject Root;

        private void Start ()
        {
            Root = this.gameObject;
            this.AddSelectionBox();
        }

        private void AddSelectionBox()
        {
            BoxCollider VolumeBox = Root.GetComponent&lt;BoxCollider&gt;();

            if (VolumeBox == null)
            {
                VolumeBox = Root.AddComponent&lt;BoxCollider&gt;();
            }

            VolumeBox.size = new Vector3(20, 20, 20);

            VolumeBox.enabled = false;
        }

    }
}
</code></pre>

<p><strong>Asset\Editor\BoxStairsEditor.cs</strong></p>

<pre><code>using UnityEngine;
using UnityEditor;

namespace BoxStairsTool
{
    [CustomEditor(typeof(BoxStairs))]
    public sealed class BoxStairsEditor : Editor
    {
        private const string DefaultName = "BoxStairs";

        [MenuItem("GameObject/3D Object/BoxStairs")]
        private static void CreateBoxStairsGO ()
        {
            GameObject BoxStairs = new GameObject(DefaultName);
            BoxStairs.AddComponent&lt;BoxStairs&gt;();

            if (Selection.transforms.Length == 1)
            {
                BoxStairs.transform.SetParent(Selection.transforms[0]);
                BoxStairs.transform.localPosition = new Vector3(0,0,0);
            }

            Selection.activeGameObject = BoxStairs;
            Undo.RegisterCreatedObjectUndo(BoxStairs, "Create BoxStairs");
        }

        public override void OnInspectorGUI ()
        {
            if (GUILayout.Button("Finalize stairs"))
            {
                FinalizeStairs();
            }
        }

        private void FinalizeStairs ()
        {
            Undo.SetCurrentGroupName("Finalize stairs");
            BoxStairs script = (BoxStairs)target;
            GameObject go = script.gameObject;
            BoxCollider bc = go.GetComponent&lt;BoxCollider&gt;();

            if (bc != null)
            {
                Undo.DestroyObjectImmediate(bc);
            }
            Undo.DestroyObjectImmediate(target);
        }
    }
}
</code></pre>