Serialized Array Initialization

  • If you have a single serialized field of a custom type in your script, Unity will properly run its field initialisers and constructor to set up its default values when you add that script to an object.
  • But if the field is an array of a custom type, then it won't ever run them.
    • The array starts empty by default.
    • If you resize it from empty to any other size, all the new elements will start at the default values for their type. For example, if you are resizing an array of MyClass which has a public float value = 5; field, then the value field will initialise to the default float value of 0 instead of executing the field initialiser to set it to 5.
    • If you increase its size after it already has elements, the new elements will all be copied from the last existing element.
  • This means that an array of Transitions like the one in the Sequence Coroutine example can't start with the proper default values so their Fade Duration and Speed will both be 0 instead of defaulting to 0.25 and 1 respectively.
    • Animancer adds a Reset Transition function to the Context Menu for any Transition fields, but there doesn't seem to be any way to properly detect and resolve this issue.
  • Bug Report Case Number: 907333 - Closed and won't fix because Unity doesn't want to fix their serialization system.