Pointer becomes null when Play mode is entered.

I’m trying to write an editor window, but I’m running into some very weird behavior when the user presses Play in the editor. Namely, some of my pointers in the window suddenly become null.

At first I thought it was because Unity was doing some funky garbage collection that didn’t like the unmanaged memory that I was referencing. But I wrote a quick and dirty test window that is getting the same error with no unmanaged memory.

Even stranger, the window’s finalizer is being called, but the window is still around and getting updates. Only pointers are being nulled - integers are not being zeroed. It looks as if the window is being cloned and destroyed, but Awake() is not being called on the clone, so my initialization code isn’t running for the new instance of the window.

What am I doing wrong here? Why does my pointer (m_other) become null? Why are the finalizers being called?

Note, this is also affecting static singletons that I have. Singletons that are NOT UnityEngine or UnityEditor objects.

`
    using UnityEngine;
    using UnityEditor;
    
    class SomethingElse
    {
        int Val;
        //FileSystemWatcher watcher;
    
        public SomethingElse()
        {
            Val = 18;
            Debug.Log("Constructing something else.");
        }
    
        ~SomethingElse()
        {
            Debug.Log("SomethingElse Finalizing: " + Val.ToString());
        }
    }
    
    sealed class TestWindow : EditorWindow
    {
        private SomethingElse m_other;
        private int m_int;
    
        [MenuItem("Window/P4U/P4U TestWindow", false, 0)]
        public static void Init()
        {
            EditorWindow.GetWindow(false, "P4U TestWindow  ", true);
        }
    
        public TestWindow()
        {
            m_int = 5;
            Debug.Log("Window constructor");
        }
    
        ~TestWindow()
        {
            Debug.Log("Window finalizer");
        }
    
        void OnDestroy()
        {
            Debug.Log("destroy");
        }
    
        void OnApplicationQuit()
        {
            Debug.Log("quit");
        }
    
        void OnDisable()
        {
            Debug.Log("disable");
        }
    
        void OnEnable()
        {
            Debug.Log("enabled");
        }
    
        void Awake()
        {
            m_other = new SomethingElse();
        }
    
        void Update()
        {
            if (m_other == null)
            {
                Debug.Log("SomethingElse is null.");
            }
            if (m_int != 5)
            {
                Debug.Log("Der");
            }
        }
    }

`

I’d say test if your pointers are null and recreate them, but if you loose your information, I guess your problem remains. I like to do it that way personnally : variable = variable ?? new Class();

For anyone that comes looking, Unity has acknowledged this as a bug and assigned it case number 445029. If this bug affects you, then keep watching for a fix and/or email Unity to find out the timeline for fixing it.

Just grab a reference to it, thusly.

void OnGUI()
{
    if (!window)
        window = EditorWindow.GetWindow<T>();
}

I can’t find the issue mentioned in the issue tracker (445029). I think this is happening because Unity hasn’t been able to serialise the class. In my case, as soon as I added the serialisable attribute to my class it worked.