- Home /
Serialization exception attempting deserialize an empty stream
I think I'm checking for length before deserializing the file, but it seems to try anyway and give me an error. I can open the file and see that's it empty. I'm not sure what I'm doing wrong. Any advice would be appreciated!
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData()
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path) && path.Length > 0)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
return null;
}
}
}
Answer by PopRockLex · Sep 21, 2019 at 07:21 PM
I'm just posting the final solution because so many people fix the errors and don't share and post to help anybody in the future. "Dymaniod" on stack overflow pointed out that I wanted to use "stream.length" instead of "path.length" because, as "WarmedxMints" pointed out above, the path will always have a greater length than 0. I still got a load error loop. The final script that worked is below. Thanks to both of you who helped!!
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public static class SaveSystem
{
public static void SaveData(PlayerCollision playerCol)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Create);
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameData LoadData(PlayerCollision playerCol)
{
string path = Application.persistentDataPath + "/player.fun";
FileStream stream = new FileStream(path, FileMode.Open);
if (File.Exists(path) && stream.Length > 0)
{
BinaryFormatter formatter = new BinaryFormatter();
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
BinaryFormatter formatter = new BinaryFormatter();
GameData data = new GameData(playerCol);
formatter.Serialize(stream, data);
stream.Close();
return data;
}
}
}
You shouldn't really need to do this as your game should never save an empty file. Why are you saving a file if there is no file to load? Why not just return a new GameData() and save it later when it has something in it?
Shouldn't you save a file then load it, can you load a file that hasn't been saved. I'm confused by your statement.
Answer by WarmedxMints · Sep 20, 2019 at 02:58 AM
You are checking the length of the string, which will always be longer than 0.
Wrap the method contents in a try-catch;
public static GameData LoadData()
{
try
{
string path = Application.persistentDataPath + "/player.fun";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameData data = formatter.Deserialize(stream) as GameData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file was not found in " + path);
return null;
}
}
catch (System.Exception e)
{
Debug.LogError("Error Loading Save " + e);
return null;
}
}
Thanks for helping! I still get an "Attempting to Deserialize an empty stream" error. Is there a better way to check to make sure the file has contents?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to save&load every Transform data of a child of a GameObject? 0 Answers
Questing Regarding Save and Load System 0 Answers