Spawn new obstacles

Keyboard Shortcuts

In this video, Jesse Freeman teaches you how to dynamically create instances of a prefab at runtime. This is helpful for making new objects, obstacles and enemies in your game inside of Unity via code. Spawning simply allows you to create these at runtime as you need them. Learning how work with GameObjects in Unity 5 is an important skills for building a 2D game.

- [Voiceover] Now that we have our obstacles set up,…we're gonna need a way to create lots of obstacles…throughout the game.…In order to do that, we're gonna make a spanner.…A spanner will basically have a timer…attached to it and when the timer runs out…it'll create a new object and continue…to do this until the game is over.…To get started, let's go into our Scripts folder…and create a new script called Spanner.…Now, let's open up the spanner.…To begin with, let's create an array…that'll contain all the different types of…prefabs our spanner can make.…

We'll make this public, game object,…type it to an array and call it prefabs.…Next, let's add a delay.…We'll make this a float,…call it delay, and set it up…for two seconds.…Finally, we'll need a way to know…if the spanner is active or not.…This way we can shut it down…when the game is over.…Let's create a public Boolean…and call it active and we'll…automatically set it to true.…Now, in our start method,…we're gonna create something…called a coroutine.…

A coroutine is a way that we can run a script…

Resume Transcript Auto-Scroll

Author

Released

8/22/2016

Learn everything you need to get started in Unity 2D game development. Unity expert Jesse Freeman builds a complete 2D game based off the endless runner genre—a unique one-button game that's not only fun to play, but also exciting to build. The end result is a reusable code base that you can extend with your own artwork and narrative.

Start watching to learn how to convert artwork into sprites, create repeating background textures with Unity's TexturePacker, and build reusable game objects such as players, obstacles, and enemies. Jesse also covers the basics of object pooling, working with custom UI components, and multiplatform publishing.