Saving player data in binary file

Hey
I need help in saving player data in a file. I have developed a game consisting of 3 different levels.
It stores game statistics i.e. gems and fruits player has, health and lives, the overall progress of player, and settings i.e. mute music and volume, etc. I have already done this using player.pref. It is working fine.

as it is not that much sure and professional to store player statistics in player preferences so now I want to save this in a binary file.

I need your kind help -

  1. It saves data in the scene i.e. “Level 3” while playing. but when I quit the game and replay the game
    data is not loaded in scene “Main Menu”.

  2. What data to store in player pref and when to use file datastorage?

Here is the code:

 public class SaveSystem : MonoBehaviour
 {
 public static SaveSystem instance;
public bool hasLoaded;
string path;
private void Awake()
{
     path = Application.persistentDataPath + "/player.dev";
    if (!File.Exists(path))
        SavePlayer();
    if (instance != null)
    {
        Destroy(gameObject);
        return;
    }
    else
    {
        instance = this;
        LoadPlayer();
        DontDestroyOnLoad(gameObject);
   }

    if(hasLoaded)
    {
    }
}
public PlayerData playerData;
private void Start()
{
  //  playerData = new PlayerData();
}
public void SavePlayer()
{
    BinaryFormatter formatter = new BinaryFormatter();

    Debug.Log(path + " Path");
    FileStream stream;
     //var serliaizer = new XmlSerializer(typeof(PlayerData));
      stream = new FileStream(path, FileMode.Create);

    PlayerData data = new PlayerData();
    formatter.Serialize(stream, data);

    stream.Close();
    Debug.Log("Data saved");

}
public PlayerData LoadPlayer()
{
    //string path = Application.persistentDataPath + "/player.dev";
    if (File.Exists(path))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        //XmlSerializer serliaizer = new XmlSerializer(typeof(PlayerData));
        FileStream stream = new FileStream(path, FileMode.Open);
        playerData = formatter.Deserialize(stream) as PlayerData;

        Debug.Log("Load ");
        hasLoaded = true;
        stream.Close();
        return playerData;
    }
    else
    {
        Debug.LogError("Save file not found in " + path);
        return null;
    }
}
void DeleteData()
{
   // string path = Application.persistentDataPath + "/player.dev";
    if (File.Exists(path))
    {
        File.Delete(path);
    } 
}

}

  [System.Serializable]
 public class PlayerData
 {
public string level;
public string difficultyLevel;
public int gemPlayerHas;
public int cherryPlayerHas;
public int health;
public int lives;
public int numOfArrows;
public int avatarSelected;
public float[] lastCheckPointPos;


 public PlayerData( )
 {
   
    lastCheckPointPos = new float[2];
    lives = PlayerMovement.lives;
     health = PlayerMovement.currentHealth;
     level = MainMenu.currentLevel;
     difficultyLevel = MainMenu.difficultyLevel;
     gemPlayerHas = GiftData.gemCount;
     cherryPlayerHas = GiftData.cherryCount;
     //lastCheckPointPos = GameMaster.

 }

}

Storing data in a binary file does not make it any more secure or professional. Actually using the BinaryFormatter makes it less professional and less secure:

Warning

The BinaryFormatter type is dangerous
and is not recommended for data
processing. Applications should stop
using BinaryFormatter as soon as
possible, even if they believe the
data they’re processing to be
trustworthy. BinaryFormatter is
insecure and can’t be made secure.

Security in this context is mainly about the attack surface for malware.

Apart from all this, you never actually load your data in your code. You have your “LoadPlayer” method which you never ever use anywhere. Apart from that your the overall approach is super confusing and dirty. Having the PlayerData class grabbing information from various static variables of other classes inside the constructor is extremely bad design. You never have the actual reverse of those assignments anyways.

You probably copied this snippet from a not completed (and outdated) tutorial without understanding a single bit of it. What do you expect to get as an answer here? We won’t write you a save system. We don’t even know what data you actually want to save.