Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(Case 977103) Reference to ScriptableObject goes NULL when creating new Scene

Discussion in '2017.3 Beta' started by Peter77, Dec 9, 2017.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Having a reference to a ScriptableObject as member-variable of an EditorWindow, turns NULL when executing "File > New Scene".



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class TestCode : EditorWindow
    7. {
    8.     // using either of those attributes does not make a difference
    9.     //[System.NonSerialized]
    10.     //[SerializeField]
    11.     MyScriptableObject m_scriptableObject;
    12.  
    13.     void OnEnable()
    14.     {
    15.         m_scriptableObject = ScriptableObject.CreateInstance<MyScriptableObject>();
    16.     }
    17.  
    18.     //void OnDisable()
    19.     //{
    20.     //    if (m_scriptableObject != null)
    21.     //    {
    22.     //        DestroyImmediate(m_scriptableObject);
    23.     //        m_scriptableObject = null;
    24.     //    }
    25.     //}
    26.  
    27.     void OnGUI()
    28.     {
    29.         if (m_scriptableObject != null)
    30.             EditorGUILayout.HelpBox("'m_scriptableObject' is NOT null, this is the expected behavior.\nNow press 'File > New Scene'.", MessageType.Info);
    31.         else
    32.             EditorGUILayout.HelpBox("'m_scriptableObject' is null.", MessageType.Error);
    33.     }
    34.  
    35.     [MenuItem("BugReport/Open TestCode Window")]
    36.     static void DoMenuItem()
    37.     {
    38.         var wnd = EditorWindow.GetWindow<TestCode>();
    39.         wnd.Show();
    40.     }
    41. }
    42.  

    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.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Now, I wonder if this is the correct behavior actually. Perhaps the instantiated ScriptableObject is stored in the Scene? It would certainly explain why changing the scene throws out the object.

    On the other hand, so many things derive from ScriptableObject that stay intact. For example EditorWindow is derived from ScriptableObject. Maybe these things are marked to never unload via HideFlag and that's why it works for EditorWindow.
     
  3. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    Is your "MyScriptableObject" class annotated with [System.Serializable]?
    I would expect that, with combined with making the field [SerializeField] or public should fix it.