Because you have marked those fields public
, this informs Unity:
“try to deserialize any data you have into them”
If there is no data saved in the prefab / scene then the list won’t be initalized either.
If anything is saved for that field, even zero GameObjects, it will initialize the list.
You should mark those fields private
if you are initializing them all in your class.
Here’s the general skinny:
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)
-
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
Be careful with those structs or they will baffle you in other ways because they are not classes:
Remember also the difference between Value Types vs Reference Types: