Week 3 | Session 3 - Random Animal Selection - April 8, 2020
Tutorial
·
Beginner
·
+10XP
·
60 mins
·
(42)
Unity Technologies

Our animal prefabs walk across the screen and get destroyed out of bounds, but they don’t actually appear in the game unless we drag them in! In this live session, we will allow the animals to spawn on their own, in a random location at the top of the screen. In order to do so, we will create a new object and a new script to manage the entire spawning process.
1. Overview
This is the same content covered in Lesson 2.3 in the independent Create with Code Course.
What you will make:
- When the user presses the S key, a randomly selected animal will spawn at a random position at the top of the screen, walking towards the player.
What you will learn:
- Create an empty object with a script attached
- Use arrays to create an accessible list of objects or values
- Use integer variables to determine an array index
- Randomly generate values with Random.Range in order to randomize objects in arrays and spawn positions
- Change the camera’s perspective to better suit your game
2. Before you Begin
To participate in this session and follow along, you need:
- Unity 2018.4 installed
- An up-to-date project open in Unity:
- If you have followed along with us this week and have your own project ready, you are good to go!
- You can also download the instructor's up-to-date project and import it (If you don't know how to import a project, you can read how here)
If this is your first Create with Code live learning session, you can catch up by:
- Following along with the recordings of our previous live learning sessions
- Completing the same lessons in the independent Create with Code Course
*Note - it is not necessary for you to catch up, but it is recommended for an ideal learning experience.
3. Recorded Live Session - 8th April - 9am PST
If you missed this live session, you can catch up with the video below:
4. Recorded Live Session - 8th April - 5pm PST
If you missed this live session, you can catch up with the video below:
5. Next Steps
Get caught up:
If you were not able to finish the session along with the instructors, you can catch up by:
- Re-watching this recorded session when it is posted in Step 3 above at 3am PT (10am UCT) the morning after this session's date
- Picking up where you left off in Lesson 2.3 of the independent Create with Code Course
Optional homework:
- Try making the player fire random foods from an array
- Give the animals random speeds within a certain range
Extra credit:
- If you want to go ahead and preview what we'll be covering in the next session, you can give it a try in Lesson 2.4 of the independent Create with Code Course.