---
title: "Lesson 5.3 - Game Over"
content_type: tutorial
url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-3-game-over-2?language=en"
language: en
container_type: pathway
container_title: "Junior Programmer"
container_url: "https://learn.unity.com/pathway/junior-programmer.md?language=en&version=6.3"
unit_title: "User Interface"
unit_url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface?language=en&version=6.3"
standalone_markdown_url: "https://learn.unity.com/tutorial/lesson-5-3-game-over-2.md?language=en&version=6.3"
previous_title: "Lesson 5.2 - Keeping Score"
previous_url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-2-keeping-score?language=en&version=6.3"
previous_markdown_url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-2-keeping-score.md?language=en&version=6.3"
next_title: "Lesson 5.4 - What’s the Difficulty?"
next_url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-4-what-s-the-difficulty-2?language=en&version=6.3"
next_markdown_url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-4-what-s-the-difficulty-2.md?language=en&version=6.3"
difficulty: beginner
duration_minutes: 70
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "6.0"
    url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-3-game-over-2.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-3-game-over-2.md?language=en&version=6.3"
  - name: "2022.3"
    url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-3-game-over-2.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/pathway/junior-programmer/unit/user-interface/tutorial/lesson-5-3-game-over-2.md?language=en&version=2021.3"
last_updated: "2026-08-04T14:23:39.000Z"
---

# Lesson 5.3 - Game Over

Create Game Over text and a Restart Game button that stop spawning targets, reset the score, and let users replay the game.

**Overview:**

We added a great score counter to the game, but there are plenty of other game-changing UI elements that we could add. In this lesson, we will create some “Game Over” text that displays when a “good” target object drops below the sensor. During game over, targets will cease to spawn and the score will be reset. Lastly, we will add a “Restart Game” button that allows the player to restart the game after they have lost.

**Project Outcome:**

When a “good” target drops below the sensor at the bottom of the screen, the targets will stop spawning and a “Game Over” message will display across the screen. Just underneath the “Game Over” message will be a “Reset Game” button that reboots the game and resets the score, so the player can enjoy it all over again.

Overview: [Video](https://connect-mediagw.unity.com/h1/20241205/videos/d7cbdfec-de52-4d2f-882b-c1da2162cc47_CWC_U5.L3.S0_Intro_U6.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20241205/8125d9ee-757c-4d21-9b18-ca5af88d4adc_CWC_U5.L3.S0_Intro_U6.mp4.vtt))

## 1. Create a Game Over text object

If we want some “Game Over” text to appear when the game ends, the first thing we’ll do is create and customize a new UI text element that says “Game Over”.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/b2808e89-6883-47d8-b1f6-e4e7ad2f3e1e_CWC_U5.L3.S1_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObo52%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObo9K%22%5D.subtitles/CWC_U5.L3.S1_U6.mp4_20260505_050857.srt))

1. Right-click on the **Canvas**, create a new _UI > **Text - TextMeshPro**_ object, and rename it “Game Over Text”
2. In the inspector, edit its **Text**, **Pos X**, **Pos Y**, **Font Asset**, **Size**, **Style**, **Color**, and **Alignment**
3. Change the **Text Wrapping** setting from **Normal** to **No Wrap**.

## 2. Make GameOver text appear

We’ve got some beautiful Game Over text on the screen, but it’s just sitting and blocking our view right now. We should deactivate it, so it can reappear when the game ends.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/72086903-7344-4869-8a39-9b1dc26c07e4_CWC_U5.L3.S2_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObpFy%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObpKG%22%5D.subtitles/CWC_U5.L3.S2_U6.mp4_20260505_050910.srt))

1. In GameManager.cs, create a new **_public TextMeshProUGUI gameOverText;_** and assign the **Game Over** object to it in the inspector
2. **Uncheck** the Active checkbox to **deactivate** the Game Over text by default
3. In **_Start()_**, activate the Game Over text

![](https://connect-mediagw.unity.com/h1/20190523/learn/images/9b4a175e-82f3-4337-b616-fddfe5eb0c41_5_3_2.png)

## 3. Create GameOver function

We’ve temporarily made the “Game Over” text appear at the start of the game, but we actually want to trigger it when one of the “Good” objects is missed and falls.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/18b07d04-99de-4db8-9c17-96029565bc01_CWC_U5.L3.S3_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObpqR%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObpuj%22%5D.subtitles/CWC_U5.L3.S3_U6.mp4_20260505_050922.srt))

1. Create a new **_public void GameOver()_** function, and **move** the code that activates the game over text inside it
2. In Target.cs, call **_gameManager.GameOver()_** if a target collides with the **sensor**
3. Add a new “Bad” tag to the **Bad object**, add a condition that will only trigger game over if it’s _not_ a bad object

![](https://connect-mediagw.unity.com/h1/20200423/learn/images/9947571a-613e-47cf-8c4b-f7ff421be254_Screen_Shot_2020_04_23_at_10.30.15_AM.png)

## 4. Stop spawning and score on GameOver

The “Game Over” message appears exactly when we want it to, but the game itself continues to play. In order to truly halt the game and call this a “Game Over’, we need to stop spawning targets and stop generating score for the player.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObqZU%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObqdm%22%5D.overviewVideo/Unit%205.3%20-%20Step%204%20-%20Stop%20spawning%20and%20score%20on%20GameOver%20%28NO%20VO%29%20-%20EDITED_5-June-26_FINAL_20260605_141705.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObqZU%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObqdm%22%5D.subtitles/CWC_U5.L3.S4_U6.mp4_20260505_050950.srt))

1. Create a new **_public bool isGameActive;_**
2. As the **first line** In **_Start()_**, set **_isGameActive = true;_** and in **_GameOver()_**, set **_isGameActive = false;_**
3. To prevent spawning, in the **_SpawnTarget()_** coroutine, change **_while (true)_** to **_while (isGameActive)_**
4. To prevent scoring, in Target.cs, in the **_OnMouseDown()_** function, add the condition **_if (gameManager.isGameActive) {}_**

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObqZU%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObrTG%22%5D.image/Untitled%202_20260603_213505.png)

## 5. Add a Restart button

Our Game Over mechanics are working like a charm, but there’s no way to replay the game. In order to let the player restart the game, we will create our first UI button

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/4f31677c-6bcb-44e7-8cb8-d6d5a6dc39f9_CWC_U5.L3.S5_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObrVP%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObrZh%22%5D.subtitles/CWC_U5.L3.S5_U6.mp4_20260505_051011.srt))

1. Right-click on the **Canvas** and _Create > UI > Button - TextMeshPro**.**_
2. Rename the button “Restart Button”
3. Temporarily **reactivate** the Game Over text in order to reposition the Restart Button nicely with the text, then **deactivate** it again
4. Select the Text child object, then edit its **Text** to say “Restart”**,** its **Font, Style,** and **Size**

![](https://connect-mediagw.unity.com/h1/20231214/learn/images/ad165faf-5de0-4496-aa48-32092f9d8310_image.png)

## 6. Make the restart button work

We’ve added the Restart button to the scene and it LOOKS good, but now we need to make it actually work and restart the game.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/785038ce-6e33-4c4e-b1a1-3426147a9542_CWC_U5.L3.S6_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObsVc%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObsZu%22%5D.subtitles/CWC_U5.L3.S6_U6.mp4_20260505_051026.srt))

1. In GameManager.cs, add **_using UnityEngine.SceneManagement;_**
2. Create a new **_public void RestartGame()_** function that reloads the current scene
3. In the **Button’s** inspector, click **+** to add a new **On Click event**, drag it in the **Game Manager** object and select the **_GameManager.RestartGame_** function

![](https://connect-mediagw.unity.com/h1/20210412/learn/images/ed4649fb-bf77-441d-9f13-e773044020df_5.3.6.png)

## 7. Show restart button on game over

The Restart Button looks great, but we don’t want it in our faces throughout the entire game. Similar to the “Game Over” message, we will turn off the Restart Button while the game is active.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/ca2e2c3e-29ef-477f-8310-a18b1dd39d57_CWC_U5.L3.S7_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/a7fd49cf-8d63-4c11-aaa9-4298ed8e029c/versions%5B_key==%226d0eadd1335f%22%5D.sections%5B_key==%22u6X2hMV4GZ96GPitLObtEf%22%5D.body%5B_key==%22u6X2hMV4GZ96GPitLObtIx%22%5D.subtitles/CWC_U5.L3.S7_U6.mp4_20260505_051039.srt))

1. At the top of GameManager.cs add **_using UnityEngine.UI;_**
2. Declare a new **_public Button restartButton;_** and assign the **Restart Button** to it in the inspector
3. **Uncheck** the “Active” checkbox for the **Restart Button** in the inspector
4. In the **_GameOver_** function, activate the **Restart Button**

![](https://connect-mediagw.unity.com/h1/20190523/learn/images/9115fc91-9457-4e48-b9e7-2d3fb2a53431_5_3_7.png)

## 8. Lesson Recap

## New Functionality

- A functional Game Over screen with a Restart button
- When the Restart button is clicked, the game resets

## New Concepts and Skills

- Game states
- Buttons
- On Click events
- Scene management Library
- UI Library
- Booleans to control game states

## Next Lesson

- In our next lesson, we’ll use buttons to really add some difficulty to our game

## Check your scripts

GameManager.cs

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

public class GameManager : MonoBehaviour
{
    public List<GameObject> targets;
    public TextMeshProUGUI scoreText;
    public TextMeshProUGUI gameOverText;
    public bool isGameActive;
    public Button restartButton;

    private float spawnRate = 1.0f;
    private int score;

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

    IEnumerator SpawnTarget()
    {
        while (isGameActive)
        {
            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;
    }

    public void GameOver()
    {
        gameOverText.gameObject.SetActive(true);
        isGameActive = false;
        restartButton.gameObject.SetActive(true);
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}
```

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 && gameManager.isGameActive)
        {
            //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);
            if (!gameObject.CompareTag("Bad")) 
            { 
                gameManager.GameOver(); 
            }
        }
    }
}
```
