---
title: "Lesson 5.4 - What’s the Difficulty?"
content_type: tutorial
url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/lesson-5-4-what-s-the-difficulty-2?language=en"
language: en
container_type: course
container_title: "Create with Code"
container_url: "https://learn.unity.com/course/create-with-code.md?language=en&version=6.0"
unit_title: "Unit 5 - User Interface"
unit_url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface?language=en&version=6.0"
standalone_markdown_url: "https://learn.unity.com/tutorial/lesson-5-4-what-s-the-difficulty-2.md?language=en&version=6.0"
previous_title: "Lesson 5.3 - Game Over"
previous_url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/lesson-5-3-game-over-2?language=en&version=6.0"
previous_markdown_url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/lesson-5-3-game-over-2.md?language=en&version=6.0"
next_title: "Challenge 5 - Whack-a-Food"
next_url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/challenge-5-whack-a-food?language=en&version=6.0"
next_markdown_url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/challenge-5-whack-a-food.md?language=en&version=6.0"
difficulty: beginner
duration_minutes: 60
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "6.0"
    url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/lesson-5-4-what-s-the-difficulty-2.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/tutorial/lesson-5-4-what-s-the-difficulty-2.md?language=en&version=6.3"
  - name: "2022.3"
    url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/lesson-5-4-what-s-the-difficulty-2.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/course/create-with-code/unit/unit-5-user-interface/tutorial/lesson-5-4-what-s-the-difficulty-2.md?language=en&version=2021.3"
last_updated: "2026-08-04T14:22:12.000Z"
---

# Lesson 5.4 - What’s the Difficulty?

Build a title screen with styled text and difficulty buttons that control target spawn rates in your finished game.

**Overview:**

It’s time for the final lesson! To finish our game, we will add a Menu and Title Screen of sorts. You will create your own title, and style the text to make it look nice. You will create three new buttons that set the difficulty of the game. The higher the difficulty, the faster the targets spawn!

**Project Outcome:**

Starting the game will open to a beautiful menu, with the title displayed prominently and three difficulty buttons resting at the bottom of the screen. Each difficulty will affect the spawn rate of the targets, increasing the skill required to stop “good” targets from falling.

Overview: [Video](https://connect-mediagw.unity.com/h1/20241205/videos/77cf5a03-fb5e-4384-b99b-6a92e2d5b1fc_CWC_U5.L4.S0_Intro_U6.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20241205/f71fc137-4f2a-4323-a7ba-78795ecdd291_CWC_U5.L4.S0_Intro_U6.mp4.vtt))

## 1. Create Title text and menu buttons

The first thing we should do is create all of the UI elements we’re going to need. This includes a big title, as well as three difficulty buttons.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/ae0fe6fc-8aec-49ca-be68-f7251fd61166_CWC_U5.L4.S1_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/3e2375b7-c8b2-4f98-b698-2caad4138796/versions%5B_key==%22VjARa8C3EIBGSbYTjQCJ2H%22%5D.sections%5B_key==%22ZnGr3kGV9xZ7apWlN0aS4Q%22%5D.body%5B_key==%22ZnGr3kGV9xZ7apWlN0aSBe%22%5D.subtitles/CWC_U5.L4.S1_U6.mp4_20260505_051145.srt))

1. Duplicate your **Game Over text** to create your **Title Text,** editing its name, text and all of its attributes
2. Duplicate your **Restart Button** and edit its attributes to create an “Easy Button” button
3. Edit and duplicate the new Easy **button** to create a “Medium Button” and a “Hard Button”

## 2. Add a DifficultyButton script

Our difficulty buttons look great, but they don’t actually do anything. If they’re going to have custom functionality, we first need to give them a new script.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/b7f1b6c3-7d88-400c-9e1a-7c8f9c00ced9_CWC_U5.L4.S2_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/3e2375b7-c8b2-4f98-b698-2caad4138796/versions%5B_key==%22VjARa8C3EIBGSbYTjQCJ2H%22%5D.sections%5B_key==%22ZnGr3kGV9xZ7apWlN0aTII%22%5D.body%5B_key==%22ZnGr3kGV9xZ7apWlN0aTPW%22%5D.subtitles/CWC_U5.L4.S2_U6.mp4_20260505_051202.srt))

1. For all 3 new buttons, in the Button component, in the **On Click ()** section, click the **minus** (**-**) button to remove the RestartGame functionality
2. Create a new **DifficultyButton.cs** script and attach it to **all 3 buttons**
3. Add  **_using UnityEngine.UI_** to your imports
4. Create a new **_private Button button;_** variable and initialize it in **_Start()_**

![](https://connect-mediagw.unity.com/h1/20190524/learn/images/4a388726-123f-40e6-9b94-a6347bf60812_5_4_2.png)

## 3. Call SetDifficulty on button click

Now that we have a script for our buttons, we can create a SetDifficulty method and tie that method to the click of those buttons

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/cc99daf8-53a5-4f52-9d38-f2227e7299aa_CWC_U5.L4.S3_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/3e2375b7-c8b2-4f98-b698-2caad4138796/versions%5B_key==%22VjARa8C3EIBGSbYTjQCJ2H%22%5D.sections%5B_key==%22ZnGr3kGV9xZ7apWlN0aUh0%22%5D.body%5B_key==%22ZnGr3kGV9xZ7apWlN0aUoE%22%5D.subtitles/CWC_U5.L4.S3_U6.mp4_20260505_051215.srt))

1. Create a new **_void SetDifficulty_** function, and inside it, **_Debug.Log(gameObject.name + " was clicked");_**
2. Add the **button listener** to call the **_SetDifficulty_** function

![](https://connect-mediagw.unity.com/h1/20190524/learn/images/d1748b11-8fee-4c78-9340-13110e9a6233_5_4_3.png)

## 4. Make your buttons start the game

The Title Screen looks great if you ignore the target objects bouncing around, but we have no way of actually starting the game. We need a StartGame function that can communicate with SetDifficulty.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/e9b71c4b-fa15-4884-ab9d-26921548bea5_CWC_U5.L4.S4_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/3e2375b7-c8b2-4f98-b698-2caad4138796/versions%5B_key==%22VjARa8C3EIBGSbYTjQCJ2H%22%5D.sections%5B_key==%22ZnGr3kGV9xZ7apWlN0aVVa%22%5D.body%5B_key==%22ZnGr3kGV9xZ7apWlN0aVco%22%5D.subtitles/CWC_U5.L4.S4_U6.mp4_20260505_051227.srt))

1. In GameManager.cs, create a new **_public void StartGame()_** function and move everything from **_Start()_** into it
2. In DifficultyButton.cs, create a new **_private GameManager gameManager;_** and initialize it in **_Start()_**
3. _In the **SetDifficulty()** function, call **gameManager.StartGame();**_

![](https://connect-mediagw.unity.com/h1/20210412/learn/images/055e6b0f-b02e-4205-8a3a-f916551fe6c9_5.4.4.png)

## 5. Deactivate Title Screen on StartGame

If we want the title screen to disappear when the game starts, we should store them in an empty object rather than turning them off individually. Simply deactivating the single empty parent object makes for a lot less work.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/a2fff210-ed68-445a-941f-a5b984941524_CWC_U5.L4.S5_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/3e2375b7-c8b2-4f98-b698-2caad4138796/versions%5B_key==%22VjARa8C3EIBGSbYTjQCJ2H%22%5D.sections%5B_key==%22ZnGr3kGV9xZ7apWlN0aWcE%22%5D.body%5B_key==%22ZnGr3kGV9xZ7apWlN0aWjS%22%5D.subtitles/CWC_U5.L4.S5_U6.mp4_20260505_051243.srt))

1. Right-click on the Canvas and _Create > Empty Object_, rename it “Title Screen”, and drag the **3 buttons** and **title** onto it
2. In GameManager.cs, create a new **_public GameObject titleScreen;_** and assign it in the inspector
3. In **_StartGame()_**, deactivate the title screen object

![](https://connect-mediagw.unity.com/h1/20190524/learn/images/274bcfae-eda9-4e30-bad8-a46782fcb0ed_5_4_5.png)

## 6. Use a parameter to change difficulty

The difficulty buttons start the game, but they still don’t change the game’s difficulty. The last thing we have to do is actually make the difficulty buttons affect the rate that target objects spawn.

[Video](https://connect-mediagw.unity.com/h1/20241206/videos/674539b8-9e58-4ad8-bbc7-cab6183fc1b9_CWC_U5.L4.S6_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/3e2375b7-c8b2-4f98-b698-2caad4138796/versions%5B_key==%22VjARa8C3EIBGSbYTjQCJ2H%22%5D.sections%5B_key==%22ZnGr3kGV9xZ7apWlN0aXmU%22%5D.body%5B_key==%22ZnGr3kGV9xZ7apWlN0aXti%22%5D.subtitles/CWC_U5.L4.S6_U6.mp4_20260505_051259.srt))

1. In DifficultyButton.cs, create a new **_public int difficulty_** variable, then in the Inspector, assign the **Easy** difficulty as 1, **Medium** as 2, and **Hard** as 3
2. Add an **_int difficulty_** parameter to the **_StartGame()_** function
3. In **_StartGame()_**, set **_spawnRate /= difficulty;_**
4. Fix the error in DifficultyButton.cs by passing the difficulty parameter to **_StartGame(difficulty)_**

![](https://connect-mediagw.unity.com/h1/20250516/learn/images/726b301b-af64-4960-a5ac-bf22bf60c763_Untitled_20.png)

## 7. Lesson Recap

## New Functionality

- Title screen that lets the user start the game
- Difficulty selection that affects spawn rate

## New Concepts and Skills

- AddListener()
- Passing parameters between scripts
- Divide/Assign (/=) operator
- Grouping child objects

## Check your scripts

DifficultyButton.cs

```csharp
using UnityEngine;
using UnityEngine.UI;

public class DifficultyButton : MonoBehaviour
{
    private Button button;
    private GameManager gameManager;
    public int difficulty;

    void Start()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(SetDifficulty);
        gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    }

    void Update()
    {
        
    }

    void SetDifficulty()
    {
        Debug.Log(gameObject.name + " was clicked");
        gameManager.StartGame(difficulty);
    }

}
```

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;
    public GameObject titleScreen;

    private float spawnRate = 1.0f;
    private int score;

    void Start()
    {
        
    }

    void Update()
    {
        
    }

    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);
    }

    public void StartGame(int difficulty)
    {
        isGameActive = true;
        score = 0;
        StartCoroutine(SpawnTarget());
        UpdateScore(0);
        titleScreen.gameObject.SetActive(false);
        spawnRate /= difficulty;
    }

}
```
