How Can I Have A Scriptable Object Inherited Serialized Property With Different Default Values?

I feel it is the BEST way actually, because when you do public int foo = 1; and then later change it to public int foo = 2; in the source code, to the average programmer just starting out in Unity it is absolutely baffling why foo still stubbornly remains 1.

And it’s such a common misunderstanding on this forum that I have a blurb! I have a lot of blurbs:

Serialized properties in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

  • what the class constructor makes (either default(T) or else field initializers, eg “what’s in your code”)

  • what is saved with the prefab

  • what is saved with the prefab override(s)/variant(s)

  • what is saved in the scene and not applied to the prefab

  • what is changed in Awake(), Start(), or even later etc.

Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

Here’s the official discussion: https://blog.unity.com/technology/serialization-in-unity

Field initializers versus using Reset() function and Unity serialization:

https://discussions.unity.com/t/829681/2

https://discussions.unity.com/t/846251/8

1 Like