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"); } } } `