I created a Saving System similar to the one in Brackey’s tutorial. It keeps data between scenes but the data gets reset after restarting the app. How can I keep the data even after restarting the game?
public class PlayerData : MonoBehaviour
{
public static bool playerisSaved;
private bool isfirstSaveExist;
public static bool itemBought{get;set;}
public static int totalCoinAmount{get;set;}
public static int selectedCar{get;set;}
public static int selectedCarColor{get;set;}
public static int selectedSpoiler{get;set;}
public static int selectedSpoilerColor{get;set;}
public static int selectedNeonColor{get;set;}
public static int selectedWheelColor{get;set;}
public int spoilerSave;
public int spoilerColorSave;
public int neonColorSave;
public int carColorSave;
public int wheelColorSave;
public int coinSave;
public int carSave;
public int currentLevel;
void Start()
{
playerisSaved = false;
if(isfirstSaveExist == true)
{
LoadPlayer();
}
}
public void SavePlayer()
{
PlayerDataSaver.SavePlayer(this);
}
public void LoadPlayer()
{
PlayerDataHandler data = PlayerDataSaver.LoadPlayer();
currentLevel = data.Level;
totalCoinAmount= data.Coin;
if(itemBought == true)
{
selectedCar = data.PickedCar;
}
}
void Update()
{
coinSave = totalCoinAmount;
carSave= selectedCar;
spoilerSave = selectedSpoiler;
spoilerColorSave = selectedSpoilerColor;
neonColorSave = selectedNeonColor;
carColorSave = selectedCarColor;
wheelColorSave = selectedWheelColor;
SaveReciver();
}
public void SaveReciver()
{
if(playerisSaved == true)
{
SavePlayer();
isfirstSaveExist = true;
playerisSaved = false;
}
}
void OnApplicationQuit()
{
SavePlayer();
}
}
[System.Serializable]
public class PlayerDataHandler
{
public int Level {get; set;}
public int Coin{get; set;}
public int PickedCar{get; set;}
public int CarColor{get; set;}
public int NeonColor{get; set;}
public int Spoiler{get; set;}
public int SpoilerColor{get; set;}
public int WheelColor{get; set;}
public static PlayerDataHandler FromPlayerData(PlayerData player)
{
return new PlayerDataHandler
{
Coin = player.coinSave,
Level = player.currentLevel,
PickedCar = player.carSave,
CarColor = player.carColorSave,
NeonColor = player.neonColorSave,
Spoiler = player.spoilerSave,
SpoilerColor = player.spoilerColorSave,
WheelColor = player.wheelColorSave,
};
}
}
using System.IO;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
public static class PlayerDataSaver
{
public static void SavePlayer(PlayerData player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.data";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerDataHandler data = PlayerDataHandler.FromPlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerDataHandler LoadPlayer()
{
string path = Application.persistentDataPath +"/player.data";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path,FileMode.Open);
PlayerDataHandler data = formatter.Deserialize(stream) as PlayerDataHandler;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in "+path);
return null;
}
}
}