[SerializeField] Not Working Properly?

I’m having an issue getting SerializeField working properly with my editor script.

I have a main class called “SAC”, I then have sever sub classes called “SACObject”, “SACInt” (which extends “SACObject”), and SACSetting. I also have an array of the SACSetting’s. (It is not initialized just like this, this is just an example). The code looks like this:

public class SAC : MonoBehaviour {

    [SerializeField]
    SACSetting[] settings = new SACSetting[] {};

    [Serializable]
    public class SACSetting {
        public SACObject val;
        public SACSetting(SACObject val){
            this.val = val;
        }
    }

    [Serializable]
    public class SACObject {}

    [Serializable]
    public class SACInt : SACObject {
        int val = 0;
        public SACInt(int val){
            this.val = val;
        }
    }
} 

Now, When I modify the “Settings” array using my custom editor script, It works just fine in editor. But, Once I press “Play” on the game, the “Val” variable in each of the SACSetting classes turns into just a SACObject instead of a SACInt… And it refuses to cast back to a SACInt… I know this seems a little confusing, But any help would be awesome. If you need any more info you can let me know.

1 Answer

1

I think if you let SACobject inherit from scriptableobject it should work. Like this:

public class SACObject : ScriptableObject {}