Challenge 2 - Play Fetch
1 Hour
For Educators
Challenge Overview:
Use your array and random number generation skills to program this challenge where balls are randomly falling from the sky and you have to send your dog out to catch them before they hit the ground. To complete this challenge, you will have to make sure your variables are assigned properly, your if-statements are programmed correctly, your collisions are being detected perfectly, and that objects are being generated randomly.
Challenge Outcome:
  • A random ball (of 3) is generated at a random x position above the screen
  • When the user presses spacebar, a dog is spawned and runs to catch the ball
  • If the dog collides with the ball, the ball is destroyed
  • If the ball hits the ground, a “Game Over” debug message is displayed
  • The dogs and balls are removed from the scene when they leave the screen
Topics we'll cover
Recommended Unity Versions
2018.4 and Above
Challenge 2 - Play Fetch
  1. Open your Prototype 2 project
  2. Download the "Challenge 2 Starter Files" from the Tutorial Materials section, then double-click on it to Import
  3. In the Project Window > Assets > Challenge 1 > Instructions folder, use the "Challenge 2 - Instructions" and “Outcome” video as a guide to complete the challenge

Dogs are spawning at the top of the screen
  • Make the balls spawn from the top of the screen
Hint: Click on the Spawn Manager object and look at the “Ball Prefabs” array

The player is spawning green balls instead of dogs
  • Make the player spawn dogs
Hint: Click on the Player object and look at the “Dog Prefab” variable

The balls are destroyed if anywhere near the dog
  • The balls should only be destroyed when coming into direct contact with a dog
Hint: Check out the box collider on the dog prefab

Nothing is being destroyed off screen
  • Balls should be destroyed when they leave the bottom of the screen and dogs should be destroyed when they leave the bottom of the screen
Hint: In the DestroyOutOfBounds script, double-check the lowerLimit and bottomLimit variables, the greater than vs less than signs, and which position (x,y,z) is being tested

Only one type of ball is being spawned
  • Ball 1, 2, and 3 should be spawned randomly
Hint: In the SpawnRandomBall() method, you should declare a new random int index variable, then incorporate that variable into the the Instantiate call

Bonus: The spawn interval is always the same
  • Make the spawn interval a random value between 3 seconds and 5 seconds
Hint: Set the spawnInterval value to a new random number between 3 and 5 seconds in the SpawnRandomBall method

Bonus: The player can “spam” the spacebar key
  • Only allow the player to spawn a new dog after a certain amount of time has passed
Hint: Search for Time.time in the Unity Scripting API and look at the example. And don’t worry if you can’t figure it out - this is a very difficult challenge.