Hi I am fairly new to coding save and load functions in Unity. I have been twiddling around with the Binary Format and eventually seem to have gotten my save files corrupted. When I tried to delete my codes and redo the script from start, I still get error messages even with scripts that worked before. Most of them relating to missing file. Is there a way to revert everything that has been altered by whatever happened through using the binary format?
Edit: Never mind I solved the problem, an on-trigger event from another script that is suppose to trigger the save function was disabled, thank you for your help.
***edit(moved from answer) ***
@the_Whizzkid
Thank you I will check out those software.
Edit:Do not know why the comment came out discorganized. I will try to fix it.
Here are 2 errors I’m currently getting:
VoicC:/Users/peter/AppData/LocalLow/DefaultComapny/ChitonIV/player.save
UnityEngine.Debug:LogError(object)
NullReferenceException: Object reference not set to an instance of an object
MovmentII.LoadPlayer() (at Assets/Script/MovmentII.cs:97)
I can’t figure out why I’m getting a Null Reference Exception because there weren’t suppose to be any object I need to attach into the inspector for that
code, it just started happening after I remove a chunk of binary script that wasn’t working.
Here is the code the NullReference Exception is targetting:
{
IEnumerator SavePlayer()
{
yield return new WaitForSeconds(3.5f);
SaveTrigger.SavePlayer(this);
}
public void LoadPlayer()
{
SaveData1 data = SaveTrigger.LoadPlayer();
Vector3 position;
position.x = data.position[0]; // The NullReference Exception points to this code but it didn't have this problem before.
position.y = data.position[1];
position.z = data.position[2];
}
This is my Binary Format script, its the same one I got from one of Brackeyes’s videos:
{
public static void SavePlayer(MovmentII player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.save";
FileStream stream = new FileStream(path, FileMode.Create);
SaveData1 data = new SaveData1(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static SaveData1 LoadPlayer()
{
string path = Application.persistentDataPath + "/player.save";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
SaveData1 data = formatter.Deserialize(stream) as SaveData1;
stream.Close();
return data;
}
else
{
Debug.LogError("Void" + path);
return null;
}
}
public static void DeleteSaveData()
{
File.Delete(Application.persistentDataPath + "/player.save");
}
}
This is one of the scripts I disabled:
{
public static void SaveMove(MovmentII movement)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/move.save";
FileStream stream = new FileStream(path, FileMode.Create);
NewMovementData data = new NewMovementData(movement);
formatter.Serialize(stream, data);
stream.Close();
}
public static NewMovementData LoadPlayer()
{
string path = Application.persistentDataPath + "/move.save";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
NewMovementData data = formatter.Deserialize(stream) as NewMovementData;
stream.Close();
return data;
}
else
{
Debug.LogError("No Save file" + path);
return null;
}
}
}
}