Custom Editor Inspector member class with inheritance, values resets after starting Play Mode

Hello, let’s say I have following classes:

public class A {
    public float a;
public class B : A {
    public float b;
public class C : A {
    public float c;
, Manager class which has class A as member:
<pre><code>using UnityEngine;
public class Manager : MonoBehaviour {
    public enum Mode { B, C };
    public Mode mode = Mode.B;
    public A classA;

and custom inspector for Manager class:

using UnityEngine;
using UnityEditor;

public class ManagerEditor : Editor {

    private string[] tabNames = { "B", "C" };
    Manager myTarget;

    private void setClass() {
        if(myTarget.mode == Manager.Mode.B) {
            if (myTarget.classA.GetType() != typeof(B)) {
                myTarget.classA = new B();
        } else if (myTarget.mode == Manager.Mode.C) {
            if(myTarget.classA.GetType() != typeof(C)) {
                myTarget.classA = new C();

    private void displayC() {
        myTarget.classA.a = EditorGUILayout.FloatField("a", myTarget.classA.a);
        (myTarget.classA as C).c = EditorGUILayout.FloatField("c", (myTarget.classA as C).c);

    private void displayB() {
        myTarget.classA.a = EditorGUILayout.FloatField("a", myTarget.classA.a);
        (myTarget.classA as B).b = EditorGUILayout.FloatField("b", (myTarget.classA as B).b);

    public override void OnInspectorGUI() {
        myTarget = target as Manager;
        myTarget.mode = (Manager.Mode)GUILayout.Toolbar((int)myTarget.mode, tabNames);

Everything seams to be fine, but when I set some values in inspector in edit mode, for example mode C, and a = 1, c = 2:

as soon as I press Play, every value resets:

Is there any way to prevent that and keep values from edit mode in play mode or I am doing something wrong?

What you’re doing wrong is that Unity’s serialization system does not support inheritance for custom serializable classes. You may want to read the section

When might the serializer behave


Note that custom editor only provide a custom visualization of the serialized data. It can’t change the way the serializer works. Custom serializable classes are serialized “inline”. The type of the instance is not saved at all, only the values. They actually behave like structs. When the class is deserialized the type of the field is used. So no matter what object you store inside the “classA” field of your Manager, after deserialization it will be an instance of “A” since that’s the type of the field.

If you need polymorphism / inheritance you have to use ScriptableObjects or MonoBehaviours, they support inheritance since those are seperately serialized objects. ScriptableObjects are meant to be saved as asset in the project. MonoBehaviours and ScriptableObject instances can be referenced by other serialized objects.