Sharing violation on path... Save Game Issue.

Hello, I am trying to make a save game system for my game but this error is making problems for it.

Here is the code: (SaveSystem.cs)

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
    public static void SaveProgress (StartGame level)
    {
        BinaryFormatter formatter = new BinaryFormatter();

        string path = Application.persistentDataPath + "/game.dat";
        FileStream stream = new FileStream(path, FileMode.OpenOrCreate);

        PlayerData data = new PlayerData(level);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static PlayerData LoadProgress()
    {
        string path = Application.persistentDataPath + "/game.dat";

        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.Log("Save file not found.");
            return null;
        }
    }
}

PlayerData.cs

using UnityEngine;

[System.Serializable]
public class PlayerData : MonoBehaviour
{
    public int GameProgress;

    public PlayerData(StartGame level)
    {
        GameProgress = level.GameProgress;
    }
}

And at last: (StartGame.cs)

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class StartGame : MonoBehaviour
{
    public int GameProgress = 0;
    public void StartGamePlay()
    {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 4);
    }

    public void BackToMenu()
    {
        SceneManager.LoadScene(1);
    }

    public void LoadLevelScene()
    {
        SceneManager.LoadScene(2);
    }

    public void LoadOptionsScene()
    {
        SceneManager.LoadScene(3);
    }

    private void Update()
    {
        int CurrentLevel = SceneManager.GetActiveScene().buildIndex;

        if(CurrentLevel == 6)
        {
            GameProgress = 1;
            Debug.Log(GameProgress + "       " + CurrentLevel);
            SaveGameProgress();
        }
    }


    public void SaveGameProgress()
    {
        SaveSystem.SaveProgress(this);
    }

    public void LoadGameProgress()
    {
        PlayerData Data = SaveSystem.LoadProgress();

        GameProgress = Data.GameProgress;
    }
    public void ContinueGame()
    {
        LoadGameProgress();
        if(GameProgress == 1)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 5);
        }
        else if (GameProgress == 2)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 6);
        }
        else
        {
            Debug.Log("Game Progress is Null");
        }
    }
}

The error I am getting when I am on scene 6 is "Sharing violation on path …“Directory” ".

This error usually happens when you are trying to open a stream to a file, that was already opened in a previous stream and not closed yet.

So in other words, the file you are trying to read/write to has a lock.

I tried using “OpenOrCreate” and other solutions but nothing worked. Also tried changing the extension, name and even change the script… yet the same problem…