Setting up a persistent high score?

I know this is not covered in the Laser Defender series, but I’m trying to make a high score feature that is saved.

I’ve watched a lot of videos on how to do it. They all use the PlayerPrefs method, but I can’t figure out how to implement it specifically into the laser defender code.

I think part of the problem may be with how things have been setup in this game, i.e… the int score variable is stored in the GameSession.cs, the score display is in the ScoreDisplay.cs, and the enemy score value and the method to add to the score when an enemy dies is in the Enemy.cs script. I’m still very new at coding and struggling how to reference all these separate items so it creates a high score feature.

If anyone has done this, or has any suggestions - I’d love to hear them. I find I’m learning a lot more by adding onto the game’s systems, and stuff like this really helps me connect the dots better. But this one is really stumping me.

Hi @matt_c,

Look for PlayerPrefs in the Unity API. That could be an idea to save your high score.

Did this help?


See also:

Thanks @Nina,

Yes, I had a look at that. Still having troubles getting it to work in the game though.

Where I’m hitting a snag is that the laser defender game has scoring variables across 3 different scripts. All the solutions on the internet show the PlayerPrefs code needs to sit in one script and pull all the needed variables - however, the tutorials on the internet have all those variables in the same script as the PlayerPrefs code.

I’m not understanding how I can reference all the codes in the other scripts, and I keep getting errors. I’ve tried the FindObjectOfType command, but I think I’m still a bit new at the coding to pull this off.

No worries, I think I’m going to move ahead in the series regardless. I imagine all these things will start to come together for me as I continue the learning process.

Maybe keep this thread open so if anyone in the future has any suggestions or feedback, they can add it in?

Do you know how to write “normal” C# classes? If so, you could ignore the Unity part and write such a class, for example, a Score or HighScore class. You could even make it static and call the PlayerPrefs methods in your own methods. Or implement the singleton pattern. It’s not that difficult. Your “normal” C# objects do not need any game objects, and you can create and access them within objects inheriting from MonoBehaviour.

If you are interested in a free C# course, I can recommend Bob Tabor’s.

Maybe keep this thread open so if anyone in the future has any suggestions or feedback, they can add it in?

Sure. We could keep the thread open for a couple of weeks. If it’s closed but you want to proceed with realising your idea, you could simply message one of the moderators who have been online in the past few days.

Thanks so much. Really appreciate your time and help :slight_smile:

A fairly non trivial and off the top of my head suggestion:
It should be possible to do the high score thing could be to just expand upon the GameSession class. It is already setup to work as a singleton and contains the score of the player. The list of scores itself would be stored using PlayerPrefs making them available between different play sessions on the same browser - assuming a webgl build.

Loading scores
At game start GameSession could load the highscores (if they exists) using a new “loadCurrentHighScores” function. This loadCurrentHighScores function could be called from within the Start of the GameSession class.

To use the loaded scores you could add a function on the GameSession class that returns the list of scores as a array and iterate over that display these wherever.

// To read back any previously stored scores
int score1 = PlayerPrefs.GetInt("topscore1", 0); //Get the value for pref named topscore1 or return 0 if not found as default
int score2 = PlayerPrefs.GetInt("topscore2", 0);
.
.
.
//and so on

Storing scores
When the player dies you ask GameSession via a new “updateHiScore” function to see if the current score is in the top x scores and update and save the list back using PlayerPrefs
You would call this new function from the Player.cs “Die” function before any of the other things in there.

You will need to sort and search the stored set of scores to add and update new scores and could do that by reading the scores into an array and using that. You would store the sorted scores using the PlayerPrefs api.

To store the scores using the PlayerPrefs api you could just use separate pref entries…like so

PlayerPrefs.SetInt("topscore1", 20);  //top score 1 
PlayerPrefs.SetInt("topscore2", 10);  // top score 2 
PlayerPrefs.SetInt("topscore3", 4);   // top score 3 etc.
.
.
.
//And so on

Thanks @codermonk, Appreciate you sharing this help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms