- Home /
Serialization Exception Error When Trying to Load
This is my first time posting so sorry for any obvious mistakes. I am attempting to save and load an integer, and it was working yesterday. The only thing I changed was attempting to save an array too, this didn't work and I reverted back all the changes and am now getting an error. This is my code:
public int waveRecord;
public void Save() {
BinaryFormatter formatter = new BinaryFormatter();
FileStream playerFile = File.Create(Application.persistentDataPath + "/playerData.dat");
PlayerStats stats = new PlayerStats();
stats.waveRecord = waveRecord;
playerFile.Close();
}
public void Load() {
if(File.Exists(Application.persistentDataPath + "/playerData.dat")) {
BinaryFormatter formatter = new BinaryFormatter();
FileStream playerFile = File.Open(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
PlayerStats stats = (PlayerStats)formatter.Deserialize(playerFile);
Debug.Log ("LOADED");
playerFile.Close();
waveRecord = stats.waveRecord;
}
}
}
[Serializable]
class PlayerStats {
public int waveRecord;
}
The problem is when I attempt to load the game I get this error:
SerializationException: serializationStream supports seeking, but its length is 0
I have no idea what this means, and I have tried searching it but have found little help. It only happens on attempting to load the game, not saving it (which appears to work fine). I am not very experienced so would appreciate any suggestions, thanks.
edit: It appears the file is completely empty even after saving, which I am guessing is why it isn't loading properly. Not sure why this is happening.
"length is 0" sounds like the file is empty. Have you checked its contents?
I just found the file on my computer, and it is in fact completely empty even after saving the game. Unfortunately I don't know what the problem is and why it wouldn't be saving properly. But thank you for pointing me in the right direction.
Answer by _dns_ · Aug 10, 2014 at 03:33 PM
Hi, I guess you didn't call formater.serialize in your Save() function.
Answer by Zovin · Aug 10, 2014 at 03:37 PM
I found the problem. I must have accidentally deleted this line from the save when I made the edits:
formatter.Serialize(playerFile, stats);
It simply wasn't saving properly, and since the file was empty then I was getting an error when I tried to load it.
Your answer
Follow this Question
Related Questions
I am getting a serialization exception error when trying to save and load in Unity? 2 Answers
Serialized Data Not Saving on Android Build 0 Answers
Can't Serialize Color or Vector2 3 Answers
Serializing a graph 2 Answers
how to properly save an int value ? 2 Answers