- Home /
subclass default values in list
Any idea why the test variable default value isn't -1 in the inspector? It's always 0 no matter what I define in the script.
[Serializable]
public class BaseClass {
public int test = -1;
}
[Serializable]
public class ListClass {
public string foo;
[Serializable]
public class SubClass : BaseClass {
public string bar;
}
public SubClass subClass = new SubClass();
}
public List<ListClass> listClass = new List<ListClass> ();
Answer by chechoggomez · Apr 08, 2015 at 02:18 AM
The inspector overwrites values of serialized items after construction. For the behavior the you want, you can set values on the constructed Data instances in Start or OnAfterDeserialize.
Take a look here:
http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnAfterDeserialize.html http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html
Answer by jinincarnate · Mar 19, 2020 at 04:21 PM
Anyone still looking might wanna take a look at this answer link to answer