Hello everybody,
hours of research and debugging couldn’t help me, which is why you are my last hope. The error “SerializationException: End of Stream encountered before parsing was completed” occurs when I try to read a file from a local folder.
I already tried:
- using “using” instead of just stream.Close();
- setting stream.position = 0
- searching for errors like serialized class not serializable or other syntax errors
The main class to save:
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveHandler
{
private static string filePath = Application.persistentDataPath + "/2401202.sav";
public static void save()
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
//fs.Position = 0;
SaveDataCollector saveData = new SaveDataCollector();
bf.Serialize(fs, saveData);
}
}
public static void load()
{
if (File.Exists(filePath))
{
BinaryFormatter bif = new BinaryFormatter();
using (FileStream fes = new FileStream(filePath, FileMode.Open))
{
//fes.Position = 0;
SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
savoDato.distributeData();
}
}
else
{
Debug.Log("Errorcode 401: Savefile not Found");
}
}
}
public static void load()
{
if (File.Exists(filePath))
{
BinaryFormatter bif = new BinaryFormatter();
using (FileStream fes = new FileStream(filePath, FileMode.Open))
{
//fes.Position = 0;
SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
savoDato.distributeData();
}
}
else
{
Debug.Log("Errorcode 401: Savefile not Found");
}
}
The serialized class:
using UnityEngine;
[System.Serializable]
public class SaveDataCollector
{
public float highScore;
public bool legacyControl;
public SaveDataCollector()
{
legacyControl = InterData.legacyControl;
highScore = InterData.highScore;
}
public void distributeData()
{
InterData.legacyControl = this.legacyControl;
InterData.highScore = this.highScore;
}
}
Please help me, I’m really getting desperate.
Thanks in advance!