Learn Understanding Prefabs and Instantiation working project make money

Understanding Prefabs and Instantiation



Instantiating and destroying objects is considered very important during gameplay. Instantiating simply means bringing into existence. Items appear or “spawn” in the game, enemies die, GUI elements vanish and scenes are loaded all the time in-game. Knowing how to properly get rid of unneeded objects and how to bring in those you do then becomes even more essential.

Let us first understand what prefabs are. Prefabs are considered important to understand how Instantiation works in Unity.

Prefabs are like blueprints of a GameObject. Prefabs are, in a way, a copy of a GameObject that can be duplicated and put into a scene, even if it did not exist when the scene was being made; in other words, prefabs can be used to dynamically generate GameObjects.

To create a prefab, you simply have to drag the desired GameObject from your scene hierarchy into the project Assets.

Prefabs Creation

Now, to instantiate a GameObject, we call the Instantiate() method in our script. This method, defined in MonoBehaviour, takes in a GameObject as a parameter, so it knows which GameObject to create/duplicate. It also has various overrides for changing the newly instantiated object’s transform, as well as parenting.

Let us try instantiating a new hexagon whenever the Space key is pressed.

Create a new script called Instantiator and open it up. In the Update method, type in the code given below.

Here, we are using the GetKeyDown method of the Input class to check if the player pressed a specific button during the last frame. Since we want it to keep checking, we put it in Update, which runs 60 times per second. The GetKeyDown method returns true if the key specified by the KeyCode enum (which lists all possible keys on a standard keyboard) is pressed in that frame.

public class Instantiator : MonoBehaviour {
   public GameObject Hexagon;
   // Update is called once per frame
   void Update () {
      if (Input.GetKeyDown(KeyCode.Space)) {
         Instantiate(Hexagon);
      }
   }
}

The public GameObject declaration at the top creates a slot similar to the one we made for the Rigidbody2D in our previous lessons. This slot only accepts prefabs (in editor time) and gameObjects (in runtime), however.

Save the script, and let it compile. Once it is done, create a new, empty GameObject by going to your object hierarchy right-click menu, and selecting Create Empty.

Create Empty

Name this Object something recognizable such as Instatiator Object and attach our newly created script to it. In the slot that shows up for the GameObject, drag in the prefab we created.

Instatiator Object

If we run the game now, pressing the Spacebar will create a new Hexagon object identical to the one we used to create the prefab. You can see each hexagon being created in the object hierarchy. The reason you cannot see them show up in the game is because for the time being, they are all being created exactly one over the other.

Hexagon object

In our next lesson, we will understand the concept of object destruction.

Leave a Reply

Your email address will not be published. Required fields are marked *