Having a reference to a ScriptableObject as member-variable of an EditorWindow, turns NULL when executing “File > New Scene”.
Test Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestCode : EditorWindow
{
// using either of those attributes does not make a difference
//[System.NonSerialized]
//[SerializeField]
MyScriptableObject m_scriptableObject;
void OnEnable()
{
m_scriptableObject = ScriptableObject.CreateInstance<MyScriptableObject>();
}
//void OnDisable()
//{
// if (m_scriptableObject != null)
// {
// DestroyImmediate(m_scriptableObject);
// m_scriptableObject = null;
// }
//}
void OnGUI()
{
if (m_scriptableObject != null)
EditorGUILayout.HelpBox("'m_scriptableObject' is NOT null, this is the expected behavior.\nNow press 'File > New Scene'.", MessageType.Info);
else
EditorGUILayout.HelpBox("'m_scriptableObject' is null.", MessageType.Error);
}
[MenuItem("BugReport/Open TestCode Window")]
static void DoMenuItem()
{
var wnd = EditorWindow.GetWindow<TestCode>();
wnd.Show();
}
}
Reproduce
- Open attached user project
- Press “Mainmenu > BugReport > Open TestCode Window”
- Notice the window displays “m_scriptableObject is NOT null”
- Press “Mainmenu > File > New Scene”
Observe the window displays “m_scriptableObject is null”
Expected
Using “File > New Scene” should not turn member variables of an EditorWindow to null.