Lesson 5.4 - What’s the Difficulty? (Copy)
Tutorial
·
Beginner
·
+10XP
·
60 mins
·
(23)
Unity Technologies

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.
Languages available:
1. Introduction
CWC_Intro_5_4
2. 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.
CWC_U5.L4.S1
- Duplicate your Game Over text to create your Title Text, editing its name, text and all of its attributes
- Duplicate your Restart Button and edit its attributes to create an “Easy Button” button
- Edit and duplicate the new Easy button to create a“Medium Button” and a “Hard Button”
3. 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.
CWC_U5.L4.S2
- For all 3 new buttons, in the Button component, in the On Click () section, click the minus (-) button to remove the RestartGame functionality
- Create a new DifficultyButton.cs script and attach it to all 3 buttons
- Add using UnityEngine.UI to your imports
- Create a new private Button button; variable and initialize it in Start()

4. 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
CWC_U5.L4.S3
- Create a new void SetDifficulty function, and inside it, Debug.Log(gameObject.name + " was clicked");
- Add the button listener to call the SetDifficulty function

5. 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.
CWC_U5.L4.S4
- In GameManager.cs, create a new public void StartGame() function and move everything from Start() into it
- In DifficultyButton.cs, create a new private GameManager gameManager; and initialize it in Start()
- In the SetDifficulty() function, call gameManager.startGame();

6. 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.
CWC_U5.L4.S5
- Right-click on the Canvas and Create > Empty Object, rename it “Title Screen”, and drag the 3 buttons and title onto it
- In GameManager.cs, create a new public GameObject titleScreen; and assign it in the inspector
- In StartGame(), deactivate the title screen object

7. 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.
CWC_U5.L4.S6
- 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
- Add an int difficulty parameter to the StartGame() function
- In StartGame(), set spawnRate /= difficulty;
- Fix the error in DifficultyButton.cs by passing the difficulty parameter to StartGame(difficulty)

8. Lesson Recap
CWC_U5.L4.SRecap
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