Search Unity

Saving and loading in binary, corrupted save help!?

Discussion in 'Editor & General Support' started by Jables17, Jan 8, 2019.

  1. Jables17

    Jables17

    Joined:
    Sep 23, 2018
    Posts:
    13
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.IO;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7.  
    8. [System.Serializable]
    9. public class SaveData {
    10.     public bool[] isPlayable;
    11.     public int[] highScore;
    12.     public int[] stars;
    13.     public int lives;
    14.     public int catnip;
    15.     public int badgeMatch;
    16.     public int badgeBreakable;
    17.     public int colorBombs;
    18.     public int columnBombs;
    19.     public int rowBombs;
    20.     public int freeSpins;
    21.     public bool adsEnabled;
    22. }
    23.  
    24. public class GameData : MonoBehaviour {
    25.  
    26.     public static GameData gameData;
    27.     public SaveData saveData;
    28.  
    29.     void Awake ()
    30.     {
    31.         if (gameData == null)
    32.         {
    33.             DontDestroyOnLoad (this.gameObject);
    34.             gameData = this;
    35.             Debug.Log ("Created Save Script");
    36.         }
    37.         else
    38.         {
    39.             Destroy (this.gameObject);
    40.         }
    41.         Load ();
    42.     }
    43.  
    44.     private void Start()
    45.     {
    46.        
    47.     }
    48.  
    49.     public void Save()
    50.     {
    51.         //Create Binary formatter to read binary files
    52.         BinaryFormatter formatter = new BinaryFormatter();
    53.  
    54.         //Create a route from the program to the files(data stream)
    55.         FileStream file = File.Create(Application.persistentDataPath + "/save.dat");
    56.  
    57.         //Create a copy of save data
    58.         SaveData data = new SaveData();
    59.         data = saveData;
    60.  
    61.         //save data
    62.         formatter.Serialize(file, data);
    63.  
    64.         //close data stream
    65.         file.Close();
    66.  
    67.         Debug.Log ("Saved Game");
    68.     }
    69.  
    70.     public void Load()
    71.     {
    72.         //Check if the save game file exists
    73.         if (File.Exists (Application.persistentDataPath + "/save.dat"))
    74.         {
    75.             //Create a Binary Formatter
    76.             BinaryFormatter formatter = new BinaryFormatter ();
    77.             FileStream file = File.Open (Application.persistentDataPath + "/save.dat", FileMode.Open);
    78.             saveData = formatter.Deserialize(file) as SaveData;
    79.             file.Close ();
    80.             Debug.Log ("Loaded File");
    81.         }
    82.         else
    83.         {
    84.             //saved variables
    85.             saveData = new SaveData ();
    86.             saveData.isPlayable = new bool[60];
    87.             saveData.stars = new int[60];
    88.             saveData.highScore = new int[60];
    89.             saveData.isPlayable [0] = true;
    90.             saveData.lives = new int ();
    91.             saveData.catnip = new int ();
    92.             saveData.badgeMatch = new int ();
    93.             saveData.badgeBreakable = new int ();
    94.             saveData.colorBombs = new int ();
    95.             saveData.columnBombs = new int ();
    96.             saveData.rowBombs = new int ();
    97.             saveData.freeSpins = new int ();
    98.             saveData.adsEnabled = new bool();
    99.             Debug.Log ("Created New Save Data");
    100.         }
    101.     }
    102.  
    103.     private void OnApplicationQuit()
    104.     {
    105.         Save ();
    106.     }
    107.  
    108.     private void OnDisable()
    109.     {
    110.         Save ();
    111.     }