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;
}
}
}
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 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;
}
}