Can I set default values for a nested class?

I have a class that applies different visual effects depending on the time of day. The different times of day are represented as a nested class, like so:

public class TimeOfDayFadeInObjects : MonoBehaviour
{
    [System.Serializable]
    public class FadeObject
    {
        public string name;
        public float maxOpacity = 1f;
        //whole bunch more stuff
    }
    public List<FadeObject> objectsToFade;
    //whole bunch more stuff
}

When I create a new instance of FadeObject in the Inspector, maxOpacity has it’s value set to 0. Is there any way to make it default to 1?

I have tried setting up a custom constructor for FadeObject, like so:

    public FadeObject()
    {
        maxOpacity = 1f;
    }

but this only seems to have the effect of bolding the values it touched in the inspector, not setting the default (I assume that Unity goes along and sets them all to a default after the constructor has run, but notices the divergence)

So, half a year later: The answer is no.

The best workaround I have found is to use Reset() to :

  • create the List
  • create an instance of FadeObject
  • then add it as the first element

and then any subsequent elements will use the “copy last element” logic built into Unity to be somewhere close to default values

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: