Quick answer: Begin OnSceneGUI with if (target == null) return;. The Unity-overloaded equality knows about destroyed objects. For always-on overlays, use SceneView.duringSceneGui with proper unsubscribe on domain reload.

After a script recompile, OnSceneGUI fires once with a stale target reference. Touching it throws “object is disposed.”

The Symptom

Editor exception in OnSceneGUI after assembly reload. Persists until you click the inspector to re-resolve target. Annoying console spam during development.

The Fix

[CustomEditor(typeof(MyComponent))]
public class MyEditor : Editor
{
    void OnSceneGUI()
    {
        if (target == null) return;
        var mc = (MyComponent)target;

        // safe to draw handles
        Handles.Label(mc.transform.position, mc.name);
    }
}

Unity overloads == on UnityEngine.Object so destroyed objects compare equal to null. Always check first.

Always-On Overlays

[InitializeOnLoad]
public static class SceneOverlay
{
    static SceneOverlay()
    {
        SceneView.duringSceneGui -= OnGui;
        SceneView.duringSceneGui += OnGui;
    }

    static void OnGui(SceneView sv) { /* draw */ }
}

Unsubscribe before subscribing prevents double-registration after assembly reload.

Verifying

Trigger an assembly reload (save a script). No exception in console. Click the GameObject; OnSceneGUI runs without error.

“Null check first. Unsubscribe before subscribe. Domain reload safe.”

Related Issues

For ScriptableObject persistence, see SO persistence. For prefab variant overrides, see variant overrides.

Null check first. Editor stays clean.