Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Discussion Disable Reload Scene Works Inconsistent

Discussion in 'Scripting' started by doublelift41, Mar 18, 2023.

  1. doublelift41

    doublelift41

    Joined:
    Aug 21, 2019
    Posts:
    5
    Reload Domain Enabled
    Reload Scene Disabled

    Unity - Manual: Scene Reloading (unity3d.com)
    1. ScriptableObject and MonoBehaviour fields that are not serialized into the build ([NonSerialized], private, or internal) keep their values. This is because Unity does not recreate existing objects and does not call constructors. Additionally, Unity converts null private and internal fields of array/List type to an empty array/List object during Domain Reload and stay non-null for runtime (non-Editor) scripts.

    In my opinion there are some unexpected situations.

    It is not mentioned in the documentation, but does not keep reference type's values except string / List<bcl types/string>.
    Its keeps int or any other bcl type but doesnt keep custom value types.
    Its keeps string value but non keep object or custom reference types. I think it might be using a Char[] approach for the string, but I'm not sure.
    Its keeps List<string/value types> but non keep List<object/custom types>

    I suspect that this is related to Domain Reloading because when Domain Reload is disabled, these values are preserved.

    Code (CSharp):
    1. public class Test: MonoBehaviour
    2. {
    3.     private int _int;
    4.     private TestStruct _struct;
    5.     private object _object;
    6.     private string _string;
    7.     private TestClass _class;
    8.     private List<object> _objectList;
    9.     private List<string> _stringList;
    10.     private List<TestClass> _classList;
    11.  
    12.     [ContextMenu("InitializeInEditor")]
    13.     private void Initialize()
    14.     {
    15.         _int = 1;
    16.         _struct = new TestStruct { X = 1 };
    17.         _object = new object();
    18.         _string = "Unity";
    19.         _class = new TestClass();
    20.         _objectList = new List<object>();
    21.         _stringList = new List<string>();
    22.         _classList = new List<TestClass>();
    23.     }
    24.  
    25.     private void Awake()
    26.     {
    27.         Debug.Log($"Int is null: {_int == 0}");
    28.         Debug.Log($"Struct is null: {_struct.X == 0}");
    29.         Debug.Log($"Object is null: {_object == null}");
    30.         Debug.Log($"String is null: {_string == null}");
    31.         Debug.Log($"Class is null: {_class == null}");
    32.         Debug.Log($"List<Object> is null: {_objectList == null}");
    33.         Debug.Log($"List<string> is null: {_stringList == null}");
    34.         Debug.Log($"List<class> is null: {_classList == null}");
    35.     }
    36.  
    37.     private struct TestStruct
    38.     {
    39.         [NonSerialized] public int X;
    40.     }
    41.  
    42.     private class TestClass
    43.     {
    44.  
    45.     }
    46. }
    Resuts
    Int is null: false
    Struct is null: True
    Object is null: True
    String is null: False
    Class is null: True

    List<Object> is null: True
    List<string> is null: False
    List<class> is null: True
     
    Last edited: Mar 18, 2023