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