Weird property default value problem under System.Serializable

So, what I want to make is a tool to assign some object a sequence of steps of animations (each step may have several animations).

And the way I do it is to have an array of instances of custom class, and the custom class has a property which stores an array of AnimationClip, and a float property determines when these animations stop, its default value is 1f which means they stop at 100% of their length.

Now the weirdness is, if I assign the array of custom class instances in the declaration using code block 1(below) it will work well, but if I only declare this array in code and let the user extend it/assign values at runtime, using code block 2(below), the float property will lose its default value and be assigned 0. And in this case the user will be very likely to forget setting it and it will look like a bug.

Here are the codes:

code block 1

[System.Serializable]
public class AnimationStep {
    public AnimationClip[] animations;
    public float playNextInPercent = 1f;
    public AnimationStep() {
    }
}

public class AnimationSequence : MonoBehaviour {
    public AnimationStep[] steps = new AnimationStep[3];
}

This will work well.

code block 2

[System.Serializable]
public class AnimationStep {
    public AnimationClip[] animations;
    public float playNextInPercent = 1f;
    public AnimationStep() {
    }
}

public class AnimationSequence : MonoBehaviour {
    public AnimationStep[] steps;
}

In this case when the user assign values to “steps” the “playNextInPercent” in each “step” will be 0, which means the playNextInPercent lost its default value of 1.

So my question is, why it loses the default value if it’s created at runtime by the user in inspector? And is there anyway to change this?

Thanks!
Pine

you need to assign the “new AnimationStep” in a constructor, then it will work

public class AnimationSequence : MonoBehaviour {
    public AnimationStep[] steps;

    public AnimationSequence () {
        steps = new AnimationStep[3];
    }
}

It will work and it is almost the same thing as “code block 1”, but it’s not what I want, because I do want to give the user the possibility to assign the value to “steps” and that’s what this tool is for. Assigning “steps” in declaration was just to show it’s not never working so it might not be the problem with System.Serializable.

So I got over this problem in a way around.

I made a custom editor for AnimationSequence, I had to set the “playNextInPercent” number using a slider anyway.

And in the custom editor I changed the “playNextInPercent” to 1 whenever it’s 0. But in this case I’m just lucky, because it’s not possible for a user to change it to 0, but in another case I can’t be sure.

It would be really good to:

  1. know why it loses default value when a new SerializedProperty is created
  2. get the time point when a new SerializedProperty is created, so as to do something with the value

Hope someone knows something about these.

Thanks!
Pine