[Persistence] Saved To File, Not Updating

I was able to properly save a highscore and load it up; However, when the player reaches a new highscore, it doesn’t update the saved value. It ends up showing the first highscore again.

public void DiedAni()
    {
        if (File.Exists(Application.persistentDataPath + "/HighscoreData.HiZarrar"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/HighscoreData.HiZarrar", FileMode.Open);
            PlayerData data = (PlayerData)bf.Deserialize(file);
            if (data.Highscore < Mathf.Floor(player.transform.position.z))
            {
                data.Highscore = Mathf.Floor(player.transform.position.z);
                bf.Serialize(file, data);
            }
            file.Close();
        }
        else
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Create(Application.persistentDataPath + "/HighscoreData.HiZarrar");
            PlayerData data = new PlayerData
            {
                Highscore = Mathf.Floor(player.transform.position.z)
            };
            bf.Serialize(file, data);
            file.Close();
        }

        StartCoroutine(YeahIDied());
    }

Looks like you’re opening the file without write permissions.

Try changing this line:
FileStream file = File.Open(Application.persistentDataPath + "/HighscoreData.HiZarrar", FileMode.Open);
For this:
FileStream file = File.Open(Application.persistentDataPath + "/HighscoreData.HiZarrar", FileMode.Open, FileAccess.Write);

If anyone could help, it’d be appreciated. I’m no longer working on this project but it would be good to know for future endeavors.