Implementing spawning
We have created lots of objects in the editor that define our level, but once the game begins, and according to the player’s actions, new objects must be created to better fit the scenarios generated by player interaction. Enemies might need to appear after a while, or bullets must be created according to the player’s input; even when enemies die, there’s a chance of spawning a power-up. This means that we cannot create all the necessary objects beforehand but should create them dynamically, and that’s done through scripting.
In this section, we will examine the following spawning concepts:
- Spawning objects
- Timing actions
- Destroying objects
We will start seeing the Unity Instantiate
function, which allows us to create instances of Prefabs at runtime, such as when pressing a key, or in a time-based fashion, such as making our enemy spawn bullets once every so often. Also, we will learn how to destroy...