---
title: "Lesson 5.2 - Keeping Score"
content_type: tutorial
url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-2-keeping-score?language=en"
language: en
container_type: course
container_title: "Unit 5 - User Interface"
container_url: "https://learn.unity.com/course/unit-5-user-interface.md?language=en&version=6.0"
standalone_markdown_url: "https://learn.unity.com/tutorial/lesson-5-2-keeping-score.md?language=en&version=6.0"
previous_title: "Lesson 5.1 - Clicky Mouse"
previous_url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-1-clicky-mouse-1?language=en&version=6.0"
previous_markdown_url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-1-clicky-mouse-1.md?language=en&version=6.0"
next_title: "Lesson 5.3 - Game Over"
next_url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-3-game-over-2?language=en&version=6.0"
next_markdown_url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-3-game-over-2.md?language=en&version=6.0"
difficulty: beginner
duration_minutes: 70
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "6.0"
    url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-2-keeping-score.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/tutorial/lesson-5-2-keeping-score.md?language=en&version=6.3"
  - name: "2022.3"
    url: "https://learn.unity.com/course/unit-5-user-interface/tutorial/lesson-5-2-keeping-score.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/tutorial/lesson-5-2-keeping-score.md?language=en&version=2021.3"
last_updated: "2026-08-04T14:24:03.000Z"
---

# Lesson 5.2 - Keeping Score

Add a scoring UI, assign point values to targets, and trigger particle explosions when objects are destroyed on click.

**Overview:**

Objects fly into the scene and the player can click to destroy them, but nothing happens. In this lesson, we will display a score in the user interface that tracks and displays the player’s points. We will give each target object a different point value, adding or subtracting points on click. Lastly, we will add cool explosions when each target is destroyed.

**Project Outcome:**

A “Score: “ section will display in the UI, starting at zero. When the player clicks a target, the score will update and particles will explode as the target is destroyed. Each “Good” target adds a different point value to the score, while the “Bad” target subtracts from the score.

Overview: [Video](https://connect-mediagw.unity.com/h1/20241205/videos/550dc0df-5348-42ec-877b-eb7c3f725a87_CWC_U5.L2.S0_Intro_U6.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20241205/f41c4030-6cc7-4b93-b9d2-65376eb25e09_CWC_U5.L2.S0_Intro_U6.mp4.vtt))

## 1. Add Score text and position it on screen

In order to display the score on-screen, we need to add our very first UI element.

[Video](https://connect-mediagw.unity.com/h1/20241205/videos/7ef1881c-dc77-4e63-af4f-5033128c815d_CWC_U5.L2.S1_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHKvD%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHL13%22%5D.subtitles/CWC_U5.L2.S1_U6.mp4_20260505_050645.srt))

1. In the **Hierarchy window**,  _right click or select + > UI > **Text - TextMeshPro**_, then, if prompted, select the button to **Import TMP Essentials**.
2. Rename the new object “Score Text”, then **zoom out** to see the **canvas** in **Scene** view.
3. Change the **Anchor Point** so that it is anchored from the **upper**-left corner.
4. In the **Inspector** window, change its **Pos X** and **Pos Y** so that it is in the upper-left corner.

## 2. Edit the Score Text’s properties

Now that the basic text is in the scene and positioned properly, we should edit its properties so that it looks nice and has the correct text.

[Video](https://connect-mediagw.unity.com/h1/20241205/videos/ba2b1d60-d1c3-4fdf-b26c-10237c155a5a_CWC_U5.L2.S2_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHMTY%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHMZO%22%5D.subtitles/CWC_U5.L2.S2_U6.mp4_20260505_050700.srt))

1. Change its text to “Score:”
2. Choose a **Font Asset**, **Style**, **Size**, and **Vertex color** to look good with your background

## 3. Initialize score text and variable

We have a great place to display score in the UI, but nothing is displaying there! We need the UI to display a score variable, so the player can keep track of their points.

[Video](https://connect-mediagw.unity.com/h1/20241205/videos/47ff4cc5-615f-4cfe-a41e-12ce7831c14e_CWC_U5.L2.S3_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHNEG%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHNK6%22%5D.subtitles/CWC_U5.L2.S3_U6.mp4_20260505_050713.srt))

1. At the top of **GameManager.cs**, add “using TMPro;**_”_**
2. Declare a new **_public_** _**TextMeshProUGUI scoreText**,_ then assign that variable in the inspector
3. Create a new **_private int score_** variable and initialize it in **_Start()_** as **_score = 0;_**
4. Also in **Start()**, set **_scoreText.text = "Score: " + score;_**

**Note:**  If your score text is split up onto two lines, change the **Text Wrapping** setting from **Normal** to **No Wrap**.

![](https://connect-mediagw.unity.com/h1/20200422/learn/images/b41684db-bf45-49d2-aac0-e327059b007c_Screen_Shot_2020_04_22_at_2.42.13_PM.png)

## 4. Create a new UpdateScore method

The score text displays the score variable perfectly, but it never gets updated. We need to write a new function that racks up points to display in the UI.

[Video](https://connect-mediagw.unity.com/h1/20241205/videos/af6d1683-63bd-4337-ab4f-4509b47c205e_CWC_U5.L2.S4_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHOpW%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHOvM%22%5D.subtitles/CWC_U5.L2.S4_U6.mp4_20260505_050727.srt))

1. Create a new **_private void UpdateScore_** method that requires one **_int scoreToAdd_** parameter
2. Cut and paste **_scoreText.text = "Score: " + score;_** into the new method, then call **_UpdateScore(0)_** in **_Start()_**
3. In **_UpdateScore()_**, increment the score by adding
   **_score += scoreToAdd;_**
4. Call **_UpdateScore(5)_** in the **spawnTarget()** function

**Note**: If your score text is split up onto two lines, change the **Text Wrapping** setting from **Normal** to **No Wrap**.

![](https://connect-mediagw.unity.com/h1/20200422/learn/images/b024e1b5-b244-48aa-9f73-c485a76191ef_Screen_Shot_2020_04_22_at_2.14.12_PM.png)

## 5. Add score when targets are destroyed

Now that we have a method to update the score, we should call it in the target script whenever a target is destroyed.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHQNr%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHQTh%22%5D.overviewVideo/Unit%205.2%20-%20Step%205%20-%20Add%20score%20when%20targets%20are%20destroyed%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260603_175522.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHQNr%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHQTh%22%5D.subtitles/CWC_U5.L2.S5_U6.mp4_20260505_050744.srt))

1. In GameManager.cs, make the **_UpdateScore_** method **_public_**
2. In Target.cs, create a reference to **_private GameManager gameManager;_**
3. Initialize GameManager in **_Start()_** using the **_Find()_** method
4. When a target is **destroyed**, call **_UpdateScore(5);_**, then **delete** the method call from SpawnTarget()

**Important**: Make sure that the name of the **Game Manager** object in the Hierarchy matches the text in **_.Find("Game Manager")_** exactly.

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHQNr%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHRkW%22%5D.image/Untitled%203_20260603_175916.png)

## 6. Assign a point value to each target

The score gets updated when targets are clicked, but we want to give each of the targets a different value. The good objects should vary in point value, and the bad object should subtract points.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHRnR%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHRtH%22%5D.overviewVideo/Unit%205.2%20-%20Step%206%20-%20Assign%20a%20point%20value%20to%20each%20target%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260603_204138.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHRnR%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHRtH%22%5D.subtitles/CWC_U5.L2.S6_U6.mp4_20260505_050758.srt))

1. In Target.cs, create a new **_public int pointValue_** variable
2. In each of the **Target prefab’s** inspectors, set the **Point Value** to whatever they’re worth, including the bad target’s **negative value**
3. Add the new variable to **_UpdateScore(pointValue);_**

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHRnR%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHSY9%22%5D.image/Untitled_20260603_204445.png)

## 7. Add a Particle explosion

The score is totally functional, but clicking targets is sort of… unsatisfying. To spice things up, let’s add some explosive particles whenever a target gets clicked!

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHSb4%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHSgu%22%5D.overviewVideo/Unit%205.2%20-%20Step%207%20-%20Add%20a%20particle%20explosion%20%28NO%20VO%29%20-%20FINAL%20RENDER_20260603_204613.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHSb4%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHSgu%22%5D.subtitles/CWC_U5.L2.S7_U6.mp4_20260505_050812.srt))

1. In Target.cs, add a new **_public ParticleSystem explosionParticle_** variable
2. For each of your target prefabs, assign a **particle prefab** from _Course Library > Particles_ to the **Explosion Particle** variable
3. In the **_OnMouseDown()_** function, **instantiate** a new explosion prefab

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/c1f9d714-fcc7-401c-8d99-f5f618984835/versions%5B_key==%22d4dd2360f336%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnHSb4%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnHTUX%22%5D.image/Untitled_20260603_205409.png)

## 8. Lesson Recap

## New Functionality

- There is a UI element for score on the screen
- The player’s score is tracked and displayed by the score text when hit a target
- There are particle explosions when the player gets an object

## New Concepts and Skills

- TextMeshPro
- Canvas
- Anchor Points
- Import Libraries
- Custom methods with parameters
- Calling methods from other scripts

## Next Lesson

- We’ll use some UI elements again - this time to tell the player the game is over and reset our game!

## Check your scripts

GameManager.cs

```csharp
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public List<GameObject> targets;
    public TextMeshProUGUI scoreText;

    private float spawnRate = 1.0f;
    private int score;

    void Start()
    {
        StartCoroutine(SpawnTarget());
        score = 0;
        UpdateScore(0);
    }

    void Update()
    {
        
    }

    IEnumerator SpawnTarget()
    {
        while (true)
        {
            yield return new WaitForSeconds(spawnRate);
            int index = Random.Range(0, targets.Count);
            Instantiate(targets[index]);
        }
    }

    public void UpdateScore(int scoreToAdd)
    {
        score += scoreToAdd;
        scoreText.text = "Score: " + score;
    }
}
```

Target.cs

```csharp
using UnityEngine;
using UnityEngine.InputSystem;

public class Target : MonoBehaviour
{
    private Rigidbody targetRb;
    private float minSpeed = 12;
    private float maxSpeed = 16;
    private float maxTorque = 10;
    private float xRange = 4;
    private float ySpawnPos = -6;
    private GameManager gameManager;
    public int pointValue;
    public ParticleSystem explosionParticle;

    void Start()
    {
        targetRb = GetComponent<Rigidbody>();
        targetRb.AddForce(RandomForce(), ForceMode.Impulse);
        targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);
        transform.position = RandomSpawnPos();
        gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    }

    void Update()
    {
        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            Debug.Log("Mouse was clicked");
            Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
            Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 2f);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                // If ray hit this enemy, destroy it
                if (hit.transform == transform)
                {
                    Destroy(gameObject);
                    Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation);
                    gameManager.UpdateScore(pointValue);
                }
            }
        }
    }

    Vector3 RandomForce()
    {
        return Vector3.up * Random.Range(minSpeed, maxSpeed);
    }

    float RandomTorque()
    {
        return Random.Range(-maxTorque, maxTorque);
    }
    
    Vector3 RandomSpawnPos()
    {
        return new Vector3(Random.Range(-xRange, xRange), ySpawnPos);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("DestroyZone"))
        {
            Destroy(gameObject);
        }
    }
}
```
