Saving Just Five Top Scores?

Hi guys, I’m a bit stuck with my lists and my savings. I want to save the date and time of the first 5 times a player reaches 10/10. After that, they can unlock a new stage in the game. What I have at the moment is this:

void DisplayResults()
    {
        resultsText.text = "You scored " + numberOfCorrectAnswers + " out 10!";

        if (numberOfCorrectAnswers == 10)
        {
            PlayerData playerData = new PlayerData();
            playerData.topScoreDates.Add(DateTime.Now.ToString());
            ES2.Save(playerData.topScoreDates, "myFile.txt?tag=myList");
        }

    }

and this:

public class PlayerData
{
    public List<string> topScoreDates;

    public PlayerData()
    {
        topScoreDates = new List<string>();
    }
}

And to retrieve the scores I have this:

        PlayerData playerData = new PlayerData();
        if (ES2.Exists("myFile.txt?tag=myList"))
            playerData.topScoreDates = ES2.LoadList<string>("myFile.txt?tag=myList");
        foreach (string score in playerData.topScoreDates)
        {
            print(score);
            scoreButton[0].GetComponentInChildren<Text>().text = score;
            scoreButton[1].GetComponentInChildren<Text>().text = score;
            scoreButton[2].GetComponentInChildren<Text>().text = score;
            scoreButton[3].GetComponentInChildren<Text>().text = score;
            scoreButton[4].GetComponentInChildren<Text>().text = score;
        }

The big problem is that what I am getting on my scoreboard is five times the same date and time. Also, this gets overwritten with each new win.

There is something about the list mechanism that I am obviously not getting. I would be grateful for some pointers. Many thanks!

In your DisplayResults() method, you are creating PlayerData object each time you get a score of 10 so you get a new list each time you instantiate your PlayerData object to which you add your date and time. And when you save your list to file, you are overwriting the current list with this new list containing one value itself.

So before you add your date and time to the file and write your list to file, you actually need to have previous values that you added to your file to be present in the list. So you need to fetch and store the previous data to the list before adding the new item to the list. Then you can add your date and time to the list and then save the list.

You can reduce the reading of file each time date needs to be saved by using a Singleton pattern for PlayerData class.