# How to add high score?

I wanted to add a high score to my game but I don't know how I already tried to find some videos for it but I just can't find the right one. I already have a scoreUI on my game I just wanted to add a high score.

Here is my UIManager's script which consists of different functions including the score system.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class uiManager : MonoBehaviour {

public Button[] buttons;
public Text scoreText;
int score;
bool gameOver;

// Use this for initialization
void Start () {
gameOver = false;
score = 0;
InvokeRepeating("scoreUpdate", 0.5f, 0.25f);
}

// Update is called once per frame
void Update () {
scoreText.text = "" + score;
}

void scoreUpdate(){
if (gameOver == false) {
score += 1;
}
}
public void gameOverActivated(){
gameOver = true;
foreach(Button button in buttons) {
button.gameObject.SetActive(true);
}
}

public void Play() {
}

}

public void Retry()
{
}

public void Pause() {

if (Time.timeScale == 1) {
Time.timeScale = 0;
}
else if (Time.timeScale == 0) {
Time.timeScale = 1;
}
}
}


I got the score system from Charger Games. Here is the link to the Video

and here is what my game looks like

• What are your requirements for your high scores table? Do you just want to keep them while the game is running (easy), do you want to persist them locally during runs (medium) or do you want an online leaderboard (hard)? Sep 26 '18 at 13:26
• I don't see any attempt to add a high score variable to your script, or to add a high score number to your UI text. Can you show us what you've tried here? Sep 26 '18 at 13:26
• @Philipp I want the high score to be saved locally and won't reset even if the game is closed. Sep 26 '18 at 13:30

To implement any form of a highscore system, you would need the following:

• A persistent storage, ie: not in memory (variables)
• A way to write to this storage
• A way to read from this storage
• A way to draw the highscore onto the screen.

You already have the last item ticked off - your high score UI objects. So let's talk about storing your score somewhere on the device.

Persistent storage

There are plenty of options to pick from. This includes writing to some file on the disk, or the cheaty way of writing it into PlayerPrefs. For the sake of a simple example, let's use PlayerPrefs to store our highscores for now.

Before writing any data to your high score storage, you would want to read from it and check it against the current score. This will determine if the highscore is to be updated. In your case, it would probably be in your gameOverActivated() function.

public void gameOverActivated(){
gameOver = true;
foreach(Button button in buttons) {
button.gameObject.SetActive(true);
}

// Check what the current highscore is
int highestScore = PlayerPrefs.GetInt("highscore", 0);
// take 0 if 'highscore' has not been set
}


Writing to storage

Once we know what the highest score is, we can decide if we want to update the score with the current score. In the same function:

if (score > highestScore) {
PlayerPrefs.SetInt("highscore", score);
}


With that done, from wherever you need to draw the highscore, just call PlayerPrefs.GetInt() with the key highscore and you have yourself an (albeit simple) highscore system.

It is important to note at this point that PlayerPrefs was never intended to be used for storing highscores. We use it in this example as it is a simple way to write to persistent storage on the device. For some further reading, you could try to write to your own file on the device, use an array of scores instead of a single score, or even look up Google Play Services to implement an online highscore/leaderboard system. In whichever way you do it, the fundamental idea remains the same.

• I can't seem to understand what you mean by calling the PlayerPrefs.GetInt("highscore"); Sorry I'm still a beginner I have just been trying to learn for the past 3 days so i don't know any much of the terminologies yet. Here is the current code: public void gameOverActivated(){ gameOver = true; foreach(Button button in buttons) { button.gameObject.SetActive(true); } int highestScore = PlayerPrefs.GetInt("highscore", 0); if (score > highestScore){ PlayerPrefs.SetInt("highscore", score); } } Sep 26 '18 at 14:38
• UPDATE: Ok I managed to make it work now but if I may ask is it possible to promp the player "New High Score!" when a new High score is reached? Sep 26 '18 at 14:45
• You have the code that checks if the current score is a high score if(score>highestScore). You can do whatever you want in there, including the prompt you're looking for. Sep 26 '18 at 14:47