Saved data reverts when ending editor session

I have a script written originally in Unity 2018 and now in Unity 2020 I have a weird bug.
I save player data to a file called playerdata.dat. When the game starts, the file is created if it’s not there and is updated when I call the savePlayerData function. I can then update values and saving the file shows an updated timestamp on the file in the folder.

Here’s the problem:
Ending the game session in the editor changes the values in the files. Even the Console values change.
For example: When hitting the play button, data is loaded in from the file. MusicVolume is 0.05. I change the value to 0 and save the data. I use Debug.Log to display the value and it shows as 0. I hit the play button and the value displayed in the Console that was 0 changes to 0.05 (in the Console. The displayed value actually changes). Hitting the play button again starts the musicVolume of playerData at 0.05 (the default). Changing the value in the editor does NOT change the displayed console value.

I’ve confirmed the “if (playerData == null)” statement never ends up being true.

public void savePlayerData()
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + "/" +"playerData.dat");
    if (playerData == null)
    {
        playerData = new PlayerData();
        playerData.wingLength = 1;
        playerData.wingWidth = 1;
        playerData.tailTrail = 1;
        playerData.tailWidth = 1;
        playerData.tailLength = 1;
        playerData.bodySize = 1;
        playerData.raceTimeRecord = 999999999;
        playerData.racePointsPerTimeRecord = 0;
        playerData.gameVolume = 1f;
        playerData.musicVolume = 0.05f;
        playerData.resolution = 123456789;
        playerData.location = 0;
        playerData.dragonColor = "yellow";
        playerData.playerID = "0123456789";
        playerData.raceIndicator = true;
        playerData.musicEnabled = true;
        playerData.muted = false;
        playerData.demonKills = 0;
        playerData.spiderKills = 0;
        playerData.crabKills = 0;
        playerData.borkGateHit = false;
        playerData.tenMinuteFlight = false;
        playerData.twentyMinuteFlight = false;
        Debug.Log("new player data created");
    }
    bf.Serialize(file, playerData);
    file.Close();
    Debug.Log("Player data saved successfully");
    Debug.Log(playerData.musicVolume.ToString());
}

public void loadPlayerData()
{
    if (File.Exists(Application.persistentDataPath + "/" + "playerData.dat"))
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.OpenRead(Application.persistentDataPath + "/playerData.dat");
        playerData = (PlayerData)bf.Deserialize(file);
        file.Close();
        Debug.Log("Player data loaded successfully");
    }
    else
    {
        this.savePlayerData();
    }
    Debug.Log(playerData.musicVolume.ToString());
}

So, why would ending a test session reset values in a saved file?

While the game was running, I copied the current version of the file and changed the name. I ended the session, saw the bug, deleted playerData.dat, and renamed the copied file to playerData.dat. When the game loaded in, the value was correct (0 which is what was saved). When I end the game session and start it again, though, it goes back to the default of 0.05.

I figured it out!!!

When you end the game session in the editor by hitting the play button, the game resets all sliders and toggles to back to what they were before the game session started.

HOWEVER, it does this while the game is running. If you have the toggles or sliders set to call a function when the value is changed, those functions will be called BEFORE the game shuts down.

I have the toggles and sliders set to update a value in the playerdata object and immediately save the data which is why all the values were being overwritten. They were being overwritten by the original values of the sliders from before the session started and not the values set in the playerdata object.