Utilizing instantiation
In this section, we will again make use of the Instantiate()
command that we saw in our earlier prototype. This is a concept that is used in many games to create projectiles, collectable objects, and even characters, such as enemies.
Instantiation is simply a method of creating (also referred to as spawning ) objects from a template (a prefab in Unity terms) during runtime—as opposed to those objects present in the scene when it loads.
The approach when using instantiation will usually take this form:
Create the game object (that you wish to instantiate in your scene) manually, adding components as necessary
Save this newly created game object as a 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 in the scene, and set the prefab you created as the object that the Instantiate() command creates by using a public variable to assign the prefab...