Introducing instantiation
In this section, we will learn how to spawn and duplicate objects while the game is running. This is a concept that is used in many games to create projectiles, collectable objects, and even characters, such as enemies.
In concept
Instantiation is simply a method of creating (also referred to as spawning) objects from a template (a prefab in Unity terms) during runtime. It can also be used to duplicate existing game objects already in the scene.
The approach when using instantiation will usually take this form:
Create the object that you wish to instantiate in your scene, and add components as necessary
Create a new prefab in your project, and drop the object you have been working on into that prefab
Delete the original object from the scene so that it is only stored as a prefab asset
Write a script that involves the
Instantiate()
command, attach it to an active game object, and set the prefab you created as the object that theInstantiate()
command creates
In code
At its...