Keeping scores is a major engaging factor in games. Players are always eager to beat that high score and to make sure no one surpasses them. Implementing a score system in your game could be the key to unlocking player engagement.
In this tutorial, we will see how to calculate scores and how to store high scores in Unity.
In most cases, the scores need to kept through different scenes. So, it’s a good practice to make the score variable a static variable, so we can update the score from other scripts across different scenes.
Unity score system: overview
Here is what we are going to do
- Create a script with the static score variable and set it as DontDestroyOnLoad().
- Update the score when the game is running based on time.
- Display the score on the screen using UI text.
- When the game ends compare the score with the high score. Save the high score to the disk.
Let’s get started.
Step 1: Declaring the score variable
First create an empty game object called “Score_System” in your very first scene. It can be a logo scene or a loading scene.
Click on Add component and add a new script called “Score_Keeper” to the game object. Open the script for editing in your code editor.
To declare a variable as static, just use the keyword static before the datatype as shown in the code below
In the Awake()
, set the score to 0 and set the script to not destroy on load.
using UnityEngine;
public class Score_Keeper : MonoBehaviour
{
public static int Score;
void Awake()
{
Score=0;
DontDestroyOnLoad(this.gameObject);
}
}
Step 2: Updating score during runtime
Let’s assume we are making a survival game and for every 10 seconds the player survives, he/she gets a point. Add the below script to a game object in your game scene.
Let’s call the script “Score_Update”.
using UnityEngine;
public class Score_Update : MonoBehaviour
{
float timer=0;
// Start is called before the first frame update
void Update()
{
timer+=Time.deltaTime;
if(timer>10)
{
Score_Keeper.Score+=1; //Updating the Score static variable
timer=0;
}
}
}
Step 3: Displaying the Score on the screen
Add a UI text to the scene by clicking on the + sign in the hierarchy window and going to UI>Text-TextMeshPro.
Place the UI wherever you want on the screen.
Set the anchors to the corner of the UI. You can watch this video on anchors, if you are new to Unity.

To access the UI text from code, we need the namespace using TMPro
. Here is the code access the text UI and update the score.
using UnityEngine;
using TMPro;
public class Score_Update : MonoBehaviour
{
[SerializeField] TMP_Text Score_display;
float timer=0;
// Start is called before the first frame update
void Update()
{
timer+=Time.deltaTime;
if(timer>10)
{
Score_Keeper.Score+=1; //Updating the Score static variable
timer=0;
Score_display.text="Score:"+Score_Keeper.Score.ToString();
}
}
}
Assing the UI text to the Score_display
variable and play the game. Now the score must get updated on the screen.
Step4: Keeping High Scores
On game over, check if the score is higher than high score. If so, set the new high score and display score. If the score is less than the high score, just display both scores.
We need one more UI to display high score. Let’s add another text object to the scene.
We will save the high score to the disk using playerprefs. Let’s create a new function called GameOver()
to compare the score and display them.
void GameOver()
{
HighScore=PlayerPrefs.GetInt("HighScore",0);//returns 0 if no high score available
if(Score_Keeper.Score>HighScore)
{
HighScore=Score_Keeper.Score;
PlayerPrefs.SetInt("HighScore",HighScore);
PlayerPrefs.Save();
}
High_score.text="HighScore: "+HighScore.ToString();
}
You just have to assign the High_score
text UI and call the GameOver()
when your game is ended.
That’s it, you have created a scoring system for your game. Now it’s your turn to test it out. If you have any other questions, feel free to leave a comment below.