Unity Create with Code Challenge 2 Solution

December 3, 2021

In challenge 2 of the Unity Create with Code course, we will create a game in which colored balls will fall from the sky while the player tries to catch the balls by spawning dogs that run across the screen. We will look at how to solve the individual problems and complete the challenge. Note that the first two steps are about setting up the project so we will be starting on step 3.

This guide is also available in video form shown below.

3. Dogs are spawning at the top of the screen

Navigate to the inspector tab for the spawn manager. You will see that the array "Ball Prefabs" contain dogs. This is causing the spawner to spawn dogs instead of balls. Replace the dogs with the balls and the spawner will work correctly.

Set the array elements to the ball prefabs

4. The player is spawning green balls instead of dogs

This step is similar to step 3. Navigate to the inspector tab for the player character. You will see that the game object "Dog Prefab" contains the prefab for the green ball. This is causing the player to spawn balls instead of dogs. Replace the ball with the dog and we will have solved the problem.

Set the field to dog prefab

5. The balls are destroyed if anywhere near the dog

This behavior is caused by the dog's collider being too big. We can edit the collider by going to the dog prefab and clicking edit collider. We then drag the box to be approximately the same size as the dog and that will solve the problem of the ball being destroyed from far away.

Drag the green box to edit the collider for the dog

6. Nothing is being destroyed off screen

If we go to the script DestroyOutOfBounds, we can see that the code is there for destroying objects off of the screen. The problem is that the conditions in the if-statements are incorrect.

For the first condition for destroying dogs, the condition checks whether a dog's x position is larger than 30. However, if we run our project and click on a dog, we see that its x position is decreasing and x = -30 is around where the dog leaves the screen. So we change the condition to be less than -30, rather than the original greater than 30.

If we run our project and click on a ball, see the that the y value of the ball is decreasing as the ball drops. In the if-statement, an object's z position is used. Simply change the z position to y position and balls will be destroyed properly.

Fix the conditions for the if-statements

7. Only one type of ball is being spawned

In the spawn manager script, we see that the instantiate function is always spawning ballPrefabs[0], which will be whatever color ball that you put in the first position of the array in step 3. We want to randomize the index we access and we can use the Random.Range method for that. Random.Range(0,2) will randomly generate 0, 1, or 2, which is what we need.

Randomize which array element is being instantiated

8. Bonus: The spawn interval is always the same

The spawn interval can also be randomized like in the step above. However, we will need to change the InvokeRepeating method as well. Instead of using InvokeRepeating, which will continue to call a function at the interval initially assigned, we can use Invoke and put it in the SpawnRandomBall method. Every time a ball spawn, a randomized timer will also start which will call the method again after a few seconds.

Randomize the spawn interval

9. Bonus: The player can “spam” the spacebar key

There are a few ways to solve this problem. The most intuitive is probably adding a timer that starts when the player spawns a dog and locking out the player from spawning another dog until that timer is up. To do this, we will create a new variable and set this variable to 1 (for a 1 second cooldown) every time we spawn a dog. In each update call we will also decrement this variable if its positive by the time that has since the last frame. Finally, we will add a condition that ensures this variable is 0 or below before we allow a dog to spawn.

Add a cooldown, think about what other ways there are to do this!


We have now completed challenge 2 and became more familiar with the Unity editor. We also worked with arrays and random numbers, both will come handy as you continue to program and make games.

If you have questions, feel free to send us a message! And if you'd like to learn about creating games or VR content, check out our Introduction to Unity Course!

Latest Articles

All Articles
There are no articles available yet.