• 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
Question by JonnyRave · Jan 08, 2019 at 01:34 AM · loadingsavingbinaryformatterbinarysave file

Saving and Loading in Binary corrupted save help

My save data script works fine on standalone. It even works on android for the most part. Out of all my friends only me and my fiance have experienced data loading corruptions on our galaxy phones. Please look at my script and let me know if there is anything I can change to fix this. I've been researching for hours and looked at the device logs with debugs over and over with no concrete solution.

What happens is after closing the game and reopening it all saved integers go to 0, even the playable levels array integers which causes every level to appear unlocked but not playable essentially crashing and corrupting the save data.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
 [System.Serializable]
 public class SaveData {
     public bool[] isPlayable;
     public int[] highScore;
     public int[] stars;
     public int lives;
     public int catnip;
     public int badgeMatch;
     public int badgeBreakable;
     public int colorBombs;
     public int columnBombs;
     public int rowBombs;
     public int freeSpins;
     public bool adsEnabled;
 }
 
 public class GameData : MonoBehaviour {
 
     public static GameData gameData;
     public SaveData saveData;
 
     void Awake () 
     {
         if (gameData == null) 
         {
             DontDestroyOnLoad (this.gameObject);
             gameData = this;
             Debug.Log ("Created Save Script");
         } 
         else 
         {
             Destroy (this.gameObject);
         }
         Load ();
     }
 
     private void Start()
     {
         
     }
 
     public void Save()
     {
         //Create Binary formatter to read binary files
         BinaryFormatter formatter = new BinaryFormatter();
 
         //Create a route from the program to the files(data stream)
         FileStream file = File.Create(Application.persistentDataPath + "/save.dat");
 
         //Create a copy of save data
         SaveData data = new SaveData();
         data = saveData;
 
         //save data
         formatter.Serialize(file, data);
 
         //close data stream
         file.Close();
 
         Debug.Log ("Saved Game");
     }
 
     public void Load()
     {
         //Check if the save game file exists
         if (File.Exists (Application.persistentDataPath + "/save.dat")) 
         {
             //Create a Binary Formatter
             BinaryFormatter formatter = new BinaryFormatter ();
             FileStream file = File.Open (Application.persistentDataPath + "/save.dat", FileMode.Open);
             saveData = formatter.Deserialize(file) as SaveData;
             file.Close ();
             Debug.Log ("Loaded File");
         } 
         else 
         {
             //saved variables
             saveData = new SaveData ();
             saveData.isPlayable = new bool[60];
             saveData.stars = new int[60];
             saveData.highScore = new int[60];
             saveData.isPlayable [0] = true;
             saveData.lives = new int ();
             saveData.catnip = new int ();
             saveData.badgeMatch = new int ();
             saveData.badgeBreakable = new int ();
             saveData.colorBombs = new int ();
             saveData.columnBombs = new int ();
             saveData.rowBombs = new int ();
             saveData.freeSpins = new int ();
             saveData.adsEnabled = new bool();
             Debug.Log ("Created New Save Data");
         }
     }
 
     private void OnApplicationQuit()
     {
         Save ();
     }
 
     private void OnDisable()
     {
         Save ();
     }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by toddisarockstar · Jan 08, 2019 at 08:01 AM

if your having problems here is a couple tools i made for myself that work every time which might be handy to convert ints to bytes and bytes to ints for saving. Getting comfortable handling and converting byte arrays yourself might be a good thing. this is a basic example for just combining the single ints. if you are interested in this approach i can repost more help to support your arrays and bools. Dont be afraid of byte arrays they don't bite. or maybe they do....but only 8 times. LOL.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.IO;
 
 public class Tools {
     public static byte[] IntsToBytes(params int[] a){
         // 4 bytes in a int
         byte[] r = new byte[a.Length*4];
         int i = 0;while (i<a.Length) {
             byte[] b = BitConverter.GetBytes(a[i]);
             int i2 = 0;while(i2<4){
                 r[(i*4)+i2]=b[i2];
                 i2++;}i++;}return r;}
     
     public static int[] BytesToInts(byte[] b){
         int[] r = new int[b.Length/4];
         int i = 0;while (i<r.Length) {
             r[i] = BitConverter.ToInt32(b,i*4);
             i++;}return r;}
 }
 
 
 [System.Serializable]
 public class SaveData {
 
     public int lives;
     public int catnip;
     public int badgeMatch;
     public int badgeBreakable;
     public int colorBombs;
     public int columnBombs;
     public int rowBombs;
     public int freeSpins;
     public byte[] bytes {get{return Tools.IntsToBytes(
                                lives,catnip,badgeMatch,
                                badgeBreakable,colorBombs,
                                columnBombs,rowBombs,freeSpins);}}
     public void loadme(byte[] b){
         int[] ii = Tools.BytesToInts (b);
         int i = 0;
         lives = ii [i];i++;
         catnip = ii [i];i++;
         badgeMatch = ii [i];i++;
         badgeBreakable = ii [i];i++;
         colorBombs = ii [i];i++;
         columnBombs = ii [i];i++;
         rowBombs = ii [i];i++;
         freeSpins = ii [i];i++;}
 }
 public class GameData : MonoBehaviour {
     public byte[] testfile;
     public SaveData saveData ;
     public SaveData saveData2 ;
 
     void Start(){
 
         testfile = saveData.bytes;
         //save and load testfile bytes to drive here;
         saveData2 = new SaveData ();
         saveData2.loadme (testfile);
 
 
 
     }
 
             
 }

  
Comment
JonnyRave

People who like this

1 Show 4 · 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
avatar image JonnyRave · Jan 08, 2019 at 08:13 PM 0
Share

is this as safe and protected as using binary saving? I'm very unfamiliar with byte saving plus if i change my save script that much I'm afraid current players would lose their saves from my current saved script. However if no one can tell me there is something wrong with my script I will definitely have to try this method or another regardless of it deleting current players save data, at least before I market it on a larger scale.

I'm hoping there is just a minor change I could make like using Path.Combine instead of File.Open but I couldn't figure out the right syntax to use it properly. I read that was a solution for a similar issue as mine.

avatar image harperrhett JonnyRave · Aug 13, 2020 at 04:29 PM 0
Share

Hey, just wondering what you ever did to fix this? I'm having a very similar problem with my save data and I'm not sure what to do.

avatar image Bunny83 harperrhett · Aug 13, 2020 at 05:06 PM 0
Share

Well, using the BinaryFormatter is not really a great solution for save games in general. If you change anything in the layout of your data (adding / removing / renaming any fields) it would break any previously saved data. Using the BinaryFormatter does not even protect the data in any way from being altered by the user. It just adds a small layer of obfuscation. The binary format of the BinaryFormatter is a well known, well documented format. So with a little bit of efford you can change any value you stored with it. Apart from that the save format is very verbose. It actually contains the whole structure of your saved class. So one could simply read out all the data without knowing anything about your game.


It would be simpler to just use a more robust format like json. You can add a simple obfuscation at the end to make it harder to read by casual users. You can not protect data on the client side completely. Though of course you still have to be careful whenever you change something in the saved data format.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 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

Related Questions

Serialized Data Not Saving on Android Build 0 Answers

I am getting a serialization exception error when trying to save and load in Unity? 2 Answers

Importing and exporting save files. 0 Answers

Can't Serialize Color or Vector2 3 Answers

Serialization Exception Error When Trying to Load 2 Answers


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