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());
}