Challenge 5 - Whack-a-Food
Tutorial
·
Beginner
·
+10XP
·
60 mins
·
(11083)
Unity Technologies

Challenge Overview:
Put your User Interface skills to the test with this whack-a-mole-like challenge in which you have to get all the food that pops up on a grid while avoiding the skulls. You will have to debug buttons, mouse clicks, score tracking, restart sequences, and difficulty setting to get to the bottom of this one.
Challenge Outcome:
- All of the buttons look nice with their text properly aligned
- When you select a difficulty, the spawn rate changes accordingly
- When you click a food, it is destroyed and the score is updated in the top-left
- When you lose the game, a restart button appears that lets you play again
1. Overview
- Open your Prototype 5 project
- Download the "Challenge 5 Starter Files" from the Tutorial Materials section, then double-click on it to Import
- In the Project Window > Assets > Challenge 5 > Instructions folder, use the Challenge 5 - Outcome video as a guide to complete the challenge
2. Warning
When you import the challenge into your project, it is supposed to have bugs.
The purpose of the challenge is for you to fix those bugs, which are listed below. There are also hints at the bottom of the page to help you if you get stuck.
If you cannot fix the bugs and wish to delete the challenge files from your project, in the Project window, right-click on Assets > Challenge 5 and select Delete.
Good luck!
3. The difficulty buttons look messy
- Center the text on the buttons horizontally and vertically
4. The food is being destroyed too soon
- The food should only be destroyed when the player clicks on it, not when the mouse touches it
5. The Score is being replaced by the word “score”
- It should always say,
“Score: __“ with the value displayed after “Score:”
6. When you lose, there’s no way to Restart
- Make the Restart button appear on the game over screen
7. The difficulty buttons don’t change the difficulty
- The spawnRate is always way too fast. When you click Easy, the spawnRate should be slower - if you click Hard, the spawnRate should be faster.
8. Bonus: The game can go on forever
- Add a “Time: __” display that counts down from 60 in whole numbers (i.e. 59, 58, 57, etc) and triggers the game over sequence when it reaches 0.
9. Hints
- Center the text on the buttons horizontally and vertically
Hint - If you expand one of the button objects in the hierarchy, you’ll see a “Text” object inside - you have to edit the properties of that “Text” object - The food should only be destroyed when the player clicks on it, not when the mouse touches it
Hint - OnMouseEnter() detects when the mouse enters an object’s collider - OnMouseDown() detects when the mouse clicks on an object’s collider - It should always say, “Score: __“ with the value displayed after “Score:”
Hint - When you set the score text, you have to add (concatenate) the word “Score: “ and the actual score value - Make the Restart button appear on the game over screen
Hint - In the GameOver() method, make sure the restart button is being reactivated - The spawnRate is always way too fast. When you click Easy, the spawnRate should be slower - if you click Hard, the spawnRate should be faster.
Hint - There is no information (or parameter) being passed from the buttons’ script to the Game Manager’s script - you need to implement a difficulty parameter - Bonus - Add a “Time: __” display that counts down from 60 in whole numbers (i.e. 59, 58, 57, etc) and triggers the game over sequence when it reaches 0.
Hint - Google, “Unity Count down timer C#”. It will involve subtracting “Time.deltaTime” and using the Mathf.Round() method to display only whole numbers.
10. Want even more challenges?
If you want, you can challenge yourself even more by attempting to add special bonus features to your main prototype, which range from Easy to Expert in difficulty.
These extra bonus challenges are entirely optional. If you are not interested and just want to continue with the course, simply mark this step complete and move on to the next tutorial.
If you are interested, check out the Bonus Feature tutorial and remember to return here when you're done to keep learning!