Challenge 5 - Whack-a-Food
1 Hour
For Educators
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
Topics we'll cover
Recommended Unity Versions
2018.4 and Above
Challenge 5 - Whack-a-Food
  1. Open your Prototype 5 project
  2. Download the "Challenge 5 Starter Files" from the Tutorial Materials section, then double-click on it to Import
  3. In the Project Window > Assets > Challenge 5 > Instructions folder, use the "Challenge 5 - Outcome” video as a guide to complete the challenge

The difficulty buttons look messy
  • 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 is being destroyed too soon
  • 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

The Score is being replaced by the word “score”
  • 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

When you lose, there’s no way to Restart
  • Make the Restart button appear on the game over screen
Hint: In the GameOver() method, make sure the restart button is being reactivated

The difficulty buttons don’t do anything
  • 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: 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.
Hint: Google, “Unity Count down timer C#”. It will involve subtracting “Time.deltaTime” and using the Mathf.Round() method to display only whole numbers.