Hello, I am using BinaryFormatter as saving tool(?) and everything works fine, but after about 2 or 3 hours of playing(testing) and opening the app my progress was just removed. I ignored that on mobile and thought that maybe it is a bug or something but now I tried to expand the game and the same thing happened in the editor. I got error that I’m trying to deserialize an empty file(probably while loading progress) and then my whole progress was gone.
I’m using code by Brackeys tutorial
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SaveGame(ClickConsequences cc, Shop shop, MainValues main)
{
Debug.Log("File Saved");
BinaryFormatter formatter = new BinaryFormatter();
string path = Path.Combine(Application.persistentDataPath + "/Click.data");
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(cc, shop);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadGame()
{
string path = Application.persistentDataPath + "/Click.data";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
[System.Serializable]
public class PlayerData
{
//Clickin Game
public int[] Level = new int[9];
public float balance;
public float prizePerClick;
public float combo100;
public int comboV;
public float critAdd;
public float critMultiplier;
public float burstAdd;
public float burstTimer;
public float prizePerPeriodMoney;
public float prizePerPeriodTime;
public PlayerData(ClickConsequences cc, Shop shop)
{
//Clickin Game
for(int i = 0; i <= 8; i++)
{
Level[i] = shop.Level[i];
}
balance = MainValues.Balance;
prizePerClick = cc.PrizePerClick;
combo100 = cc.Combo100;
comboV = cc.ComboV;
critAdd = cc.CritAdd;
critMultiplier = cc.CritMultiplier;
burstAdd = cc.BurstAdd;
burstTimer = cc.BurstTimer;
prizePerPeriodMoney = cc.PPPMoney;
prizePerPeriodTime = cc.PPPSlider.maxValue;
}
}
public void Load()
{
PlayerData data = SaveSystem.LoadGame();
if (data != null)
{
for (int i = 0; i <= 8; i++)
{
shop.Level[i] = data.Level[i];
}
MainValues.Balance = data.balance;
PrizePerClick = data.prizePerClick;
Combo100 = data.combo100;
ComboV = data.comboV;
CritAdd = data.critAdd;
CritMultiplier = data.critMultiplier;
BurstAdd = data.burstAdd;
BurstTimer = data.burstTimer;
PPPMoney = data.prizePerPeriodMoney;
PPPSlider.maxValue = data.prizePerPeriodTime;
Debug.Log("File Loaded");
}
}
As I said, everything worked fine and then when I tried to load scene by SceneManagement, it paused and showed me that error. Thank you for any help or advise