BinaryFormatter Deleted Save?

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

One thing to consider is that it might be possible that your load and save function are being called nearly simultaneously. If that’s happening, then the load function might be loading the file after it’s open for writing but before the stream is closed and actually written to disk, in which case it could see an empty file.

If you add these lines to your load function

                    FileInfo fi = new FileInfo(eventArgs.FullPath);
                    Debug.Log("Loading; file size is " + fi.Length);

If you ever see it loading with a file size of 0, you know that that’s when your problem is happening. You can sprinkle in a bunch of other Debug.Log’s to see what is going on when it’s 0. In particular, definitely put logs at the beginning and end of your saving and loading code, see if they overlap somehow or maybe are being called extremely close together (I’m not sure if there’s a possibility of a fraction-of-a-second filesystem lag).