I am trying to save/persist some data between my scenes but I keep getting the error:
SerializationException: Type UnityEngine.MonoBehaviour in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.
Here is my script:
public static void SaveState()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/EmpireGame", FileMode.OpenOrCreate);
SaveClass sc = new SaveClass()
{
food = Player.food.amount,
wood = Player.wood.amount,
iron = Player.iron.amount,
stone = Player.stone.amount,
buildingList = new List<Building>(),
};
sc.buildingList = playerBuilt;
Debug.Log("State Saved!");
bf.Serialize(file, sc);
file.Close();
}
}
[Serializable]
class SaveClass
{
public int food, wood, iron, stone;
public List<Building> buildingList;
}
I've narrowed it down to: public List buildingList; being the offending code, but I don't know how else to save this list. I think the MonoBehaviour part of it is the "" as it inherits from MonoBehaviour.
I've read:
Watched:
persistence-saving-and-loading-data
I've looked up so many Q&A's over this but still can't figure it out.
Building
class is not serializable? \$\endgroup\$