Saving generic list of several instances of a class that inherit from another class

I keep getting an error that tells me that my CharacterSheet class needs to be serializable when I try to save and whenever I put [system.serializable] at the top of the class, it gives me a similar error that the class that my CharacterSheet inherits from also needs to be serializable. I put [System.Serializable] at the top of it and then I get another error that monobehaviour needs to be serializable. I am sure that I am doing something wrong but I cant figure out what after hours of googling.

Save/Load Script:

public class CharacterManager : MonoBehaviour
{
    public static CharacterManager characterManager;
   
    void Awake () {
        if (characterManager == null) {
            characterManager = this;
            DontDestroyOnLoad (gameObject);
        }else if(characterManager != this){
            Destroy(gameObject);
        }
    }

    public List<GameObject> CharactersSaveAndLoad = new List<GameObject>(2);
    public List<CharacterSheet> CharacterSheetsSaveAndLoad = new List<CharacterSheet>(2);

    public void GetSheets(){
        CharacterSheetsSaveAndLoad.Clear();
        for (int i = 0; i < CharactersSaveAndLoad.Count; i++) {
            CharacterSheetsSaveAndLoad.Add (CharactersSaveAndLoad[i].GetComponent<CharacterSheet>());
        }
    }
    public void Save(){
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");

        GetSheets ();
        CharacterData data = new CharacterData ();

        data.Sheets = CharacterSheetsSaveAndLoad;
        bf.Serialize (file, data);
        file.Close();
        Debug.Log ("Save Compleated");
       
    }
    public static void Load(GameObject CharacterObject){
        CharacterSheet objectCharacterSheet = CharacterObject.GetComponent<CharacterSheet>();
        //CharacterSheet findCharacterSheet = CharacterObject.GetComponent<CharacterSheet>();

        if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) {
            BinaryFormatter bf = new BinaryFormatter ();
            FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            CharacterData data = (CharacterData)bf.Deserialize(file);
            file.Close();

            for (int i = 0; i < data.Sheets.Count; i++){
                if (data.Sheets[i].objectID == objectCharacterSheet.objectID){
                    //findCharacterSheet = data.Sheets[i];
                    objectCharacterSheet.characterName = data.Sheets[i].characterName;//findCharacterSheet.characterName;
                }
            }

        }
    }
}

[System.Serializable]
public class CharacterData{
    public List<CharacterSheet> Sheets;

}

It is impossible to serialize instances of types that inherit from MonoBehaviour with the BinaryFormatter because MonoBehaviour does not have a Serializable-attribute applied. You need to transfer all the data you want to save from the CharacterSheet instances into new serializable container class instances and then serialize these.

class CharacterSheet : MonoBehaviour
{
   public string name;
   public int intelligence;
}

[Serializable]
class CharacterSheetContainer
{
   public string name;
   public int intelligence;

   public CharacterSheetContainer(CharacterSheet charSheet)
   {
      this.name = charSheet.name;
      this.intelligence = charSheet.intelligence;
   }
}
1 Like

Thank you. This is exactly what I needed. Your help is very much appreciated.