System.Serializable, Constructors, Defaults

I was just wondering what the best practice was for constructors and custom classes. Currently I have a MonoBehaviour that contains a List that contains a CustomClassWithoutSerializableAttribute. I want to know the best way to default the third class. It looks like the following:

Character.cs

public class Character : MonoBehaviour
{
    public List<Ability> m_abilities;
}

Ability.cs

[System.Serializable]
public Ability : IAbility
{
    [SerializeField]
    private int m_somethingSetInEditor;

    private Damage m_damage;

    public Ability()
    {
        m_damage = new Damage(10);
    }
}

Damage.cs

public Damage
{
    public Damage(int number)
    {
    }
}

Damage is null at the end of this chain of events. I’ve been trying to find the best practice for this. Eventually I want to move Ability out of the Inspector, but for now, I want to be able to iterate quickly on this and want to know the best practice for this type of thing.

Should Ability be deriving from ScriptableObject and initializing m_damage in the Start() function?

The question is old, but since it got bumped:

I can’t reproduce your problem. The Damage instance is not null. My test case:

[System.Serializable]
public class Ability
{
    [SerializeField]
    private int m_somethingSetInEditor;

    private Damage m_damage;
    public Damage damage { get { return m_damage; } }

    public Ability()
    {
        m_damage = new Damage(10);
    }
}
public class Damage
{
    public int val;
    public Damage(int number)
    {
        val = number;
    }
}

public class Character : MonoBehaviour
{
    public List<Ability> m_abilities;
    void Start ()
    {
        foreach (var a in m_abilities)
        {
            Debug.Log(a.damage.val);
        }
    }
}

This prints out “10” for each ability that i “create” in the inspector. Though be aware that Unity does not support inheritance for custom serializable classes. So your Ability array can only contain Ability instances if you want Unity to serialize them. You can not store derived classes in that List. Well, you can but they won’t be serialized. If you need inheritance you have to use ScriptableObject and create seperate assets for each ability. In that case inheritance would work.