Question by
NinjaboyGames · Dec 06, 2017 at 12:28 AM ·
scripting problemserializationloadingsavingclasses
Can't figure out how to serialize my save data...
Hello, I'm currently trying to figure out why my save data isn't saving correctly. It's currently writing a blank file with nothing inside of it, and I can't figure out why. Here's the code:
public void Save(int slot) //4 slots
{
PlayerPrefs.SetInt("SaveSlot",saveSlot);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/save1.VortrusSave");
SaveData data = new SaveData();
data.VoidSectorsUnlocked[slot] = VoidSectorsUnlocked;
data.VenetianClusterUnlocked[slot] = VenetianClusterUnlocked;
data.VoidSectorBestScore[slot] = voidSectors_Best;
data.VenetianClusterBestScore[slot] = venetianCluster_Best;
data.AfterburnerUnlocked[slot] = AfterburnerUnlocked;
data.StarrunnerUnlocked[slot] = StarrunnerUnlocked;
bf.Serialize(file,data);
file.Close();
}
public void Load(int slot)
{
if (File.Exists(Application.persistentDataPath + "/save1.VortrusSave"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/save1.VortrusSave", FileMode.Open);
SaveData data = (SaveData)bf.Deserialize(file);
Debug.Log("It exists...");
file.Close();
VoidSectorsUnlocked = data.VoidSectorsUnlocked[slot];
VenetianClusterUnlocked = data.VenetianClusterUnlocked[slot];
voidSectors_Best = data.VoidSectorBestScore[slot];
venetianCluster_Best = data.VenetianClusterBestScore[slot];
AfterburnerUnlocked = data.AfterburnerUnlocked[slot];
StarrunnerUnlocked = data.StarrunnerUnlocked[slot];
}
}
[Serializable]
class SaveData
{
//Save names
public string[] SaveNames;
//Unlocked Levels
public int[] VoidSectorsUnlocked;
public int[] VenetianClusterUnlocked;
//Unlocked spaceships
public int[] StarrunnerUnlocked;
//Unlocked spaceship parts
public int[] AfterburnerUnlocked;
//Saved scores
public int[] VoidSectorBestScore;
public int[] VenetianClusterBestScore;
}
Comment
Is it because SaveData isn't public? It all looks fine to me!
Answer by NinjaboyGames · Mar 06, 2018 at 06:45 AM
Figured it out for anyone reading - the serialization does not allow for expandable data types so those arrays are not allowed. For example, int is allowed, but int[] is not.
Your answer
Follow this Question
Related Questions
Setting components variables with another component 0 Answers
Instantiating objects only after a scene is loaded 1 Answer
Loading and Saving error 1 Answer
Variables changes even on pause 0 Answers