PlayerPrefs to save high score for an App / Test

hi guys,

I’m having some trouble in getting my app to record a high score. It’s essentially a “test” app - I have a number of individual scenes with questions on them, and I’m using a singleton to keep score as you progress through the different scenes/questions.

At the “End Screen” I have a ScoreConverter game object and script, which takes the currentscore in the singleton, converts it to your finalscore, and depending on what the finalscore is, gives a star rating / message etc.

The problem: Every time I run through the test, the “high score” i’m displaying is just the current tests score. It doesnt seem to persist the score between test sessions. I also need to make sure that this persist through usage of the app until it’s reset (still have to code this last part).

public class ScoreConverter : MonoBehaviour
{

    [SerializeField] TextMeshProUGUI finalscore;
    [SerializeField] TextMeshProUGUI percentResult;
    [SerializeField] TextMeshProUGUI feedbackMessage;
    public float YourScore;
    float newHighScore;
    public Text highScore;
  

    void Start()
    {

        //Calculate the final score from the currentScore in the Singleton
        YourScore = FindObjectOfType<GameStatus>().currentScore;
        finalscore.text = (YourScore.ToString() + " out of 25");

        //Save New High Scores

        if (YourScore > PlayerPrefs.GetFloat("HighScore", 0))
        {
            newHighScore = YourScore;
            PlayerPrefs.SetFloat("HighScore", newHighScore);
            highScore.text = newHighScore.ToString();
        }

        // Covert the final score into a % based on 50 questions
        float Percent = YourScore * 4;
        percentResult.text = (Percent.ToString() + "%");

I should note that I’ve tried this a couple of different ways including without the newHighScore value. I followed the Brackeys YouTube tutorial and the unity documentation is a bit lite on PlayerPrefs so I reckon I must be missing something. Any help would be super appreciated.

Thanks,
MG

It certainly seems very reasonable what you are doing, unless I’m just mis-seeing it.

Try these two things:

  1. put in a PlayerPrefs.Save() right after you do the PlayerPrefs.SetFloat()

I don’t think that will fix it, but just in case something isn’t serializing right in the editor when you stop it…

  1. put lots of Debug.Log() statements in to find out when and where the compare-and-store code is running, what its values are, etc.

ALSO: if you implement a singleton wrong, you could end up with multiple instances of your ScoreConverter running, which could have really bizarre consequences as they fight it out between themselves.

1 Like

Do you notice anything slightly off about these two calls?
PlayerPrefs.GetFloat("Highscore", 0)``PlayerPrefs.SetFloat("HighScore", newHighScore)

Your capitalization is not consistent. In the future use a const string as your PlayerPrefs key to avoid typos like that. That way the compiler will yell at you if you make a typo:

const string HighScoreKey = "HighScore";

...

PlayerPrefs.GetFloat(HighScoreKey, 0);

...

PlayerPrefs.SetFloat(HighScoreKey, newHighScore);
3 Likes

Line 21: Highscore
Line 24: HighScore

They have to match exactly. Consider storing such things in a variable at the top of your script.

private readonly string highscorePref = "HighScore"

PlayerPrefs.SetFloat(highscorePref, newHighScore);
3 Likes

Thanks. I think my implementation of the Singleton is ok as I check if there are others running before i start the test. I noticed after I posed this that there is a typo in one of the "Highscore’ values :face_with_spiral_eyes: So Ive updated that and retested it, but now it sets the subsequent score to 0 sigh

Ok so I added an else statement and this problem appears to be solved. You know I tried to work this out for over an hour and essentially spotted the type about 3 minutes after posting the code, and came up with the else statement to finish it off a few more minutes after testing…

Thanks you guys for the quick responses, and aslo for the tips and advise to avoid this in future. Much obliged.

1 Like