Editor not showing default values

Hello. I have the following classes:

[System.Serializable]
class A
{
    public float a = 50f;
    public float b = 100f;
    //rest of class...
}

[System.Serializable]
class B
{
    public A[] myArray;
    //rest of class...
}

class C : MonoBehaviour
{
    public B myB;
    //rest of class...
}

For some reason every time I create a new element for “myArray” in the editor, it gets created with a=0 and b=0, and I would want to have the default values 50f and 100f.
Has anyone encountered this and know what causes it? I should also mention that I have a custom editor for class C that just adds some buttons like so:

[CanEditMultipleObjects]
[CustomEditor(typeof(C))]
public class C: Editor
{

    public override void OnInspectorGUI()
    {
        if(GUILayout.Button("Do something"))
        {
            //....
        }
        base.OnInspectorGUI();
     }
}

Thank you for your time :slight_smile:

@n_rusev, you have constructors in each of your classes, and you are calling the constructors to create instances of them? And you are creating new instances of A and adding them to your array in class B?

Assuming the containing class is a MonoBehaviour or ScriptableObject, there’s a workaround for this to automatically replace editor-created instances with correctly constructed instances.

[System.Serializable]
public class MyPlainClass {
    public float a = 100;
    public bool isInitialized = true;
}

public class MyContainerClass : MonoBehavior, ISerializationCallbackReceiver {
    public MyPlainClass[] myArray;

    public void OnBeforeSerialize(){
        for (int i=0; i<myArray.Length; ++i){
            if (!myArray_.isInitialized) myArray *= new MyPlainClass();*_

}
}
}