• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DrowningGeodude · Dec 26, 2021 at 07:05 AM · databasefilesave data

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


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image the_Whizzkid · Dec 27, 2021 at 04:48 PM 0
Share

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.

avatar image DrowningGeodude the_Whizzkid · Dec 28, 2021 at 01:06 AM 0
Share

Yes it actually was, thank you for your help. :)

Much appreciate your time.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access a text file from a built unity generated android application 0 Answers

Saving Data to Network Drive 1 Answer

Using a .txt file like a small database 1 Answer

How can I save my game data 1 Answer

Open custom file type 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges