- Home /
How to revert everything altered by the Binary Formatter?
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;
}
}
}
}
It's hard to read the code, as you mentioned. Anyways, I would try your code like this to check, I guess "data" has a null reference:
SaveData1 data = SaveTrigger.LoadPlayer();
Debug.Log("data: " + data);
Vector3 position;
Probably your "SaveTrigger.LoadPlayer();" has a problem. You might want to run the debugger step by step if you use Visual Studio. You can find those errors quite easily like this.
If not just fill the code with lots of debug.log lines to see what happens and where the null is starting to appear. Something isn't filled properly. Hope this helps.
Yes it actually was, thank you for your help. :)
Much appreciate your time.
Answer by the_Whizzkid · Dec 26, 2021 at 12:17 PM
Not as far as I know, but I didn't use the binary format yet. This might not help for now, but for the future I do recommend a Versioning Software like SVN & Tortoise or Github, so you can do easy "Backups" and even see in detail what changed between the saves in your code. This saved me already several time from spending days or weeks to find what changed or worse to rewrite everything.
If you still need help on your issue, you might want to post some of your errors, this makes it more easy to help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613050938im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to properly handle encryption for files? 0 Answers
How to fix lost data when i update the apk 0 Answers
Android: Reading another app's database? 0 Answers
Manage data on Browser 0 Answers
Write/Read save files on Windows: MyDocuments vs persistentDataPath 2 Answers