Unity - GameObject Destruction


Advertisements

The destruction of GameObjects is as important as the instantiation. In this chapter, we will learn how to destroy the GameObjects.

Fortunately, destroying GameObjects is as easy as it is creating them. You simply need a reference to the object to be destroyed, and call the Destroy() method with this reference as a parameter.

Now, let us try to make 5 hexagons which will destroy themselves when an assigned key is pressed.

Let us make a new script called HexagonDestroyer and open it in Visual Studio. We will start by making a public KeyCode variable. A KeyCode is used to specify a key on a standard keyboard, and the Input class in its methods uses it. By making this variable public, as we did with Rigidbody and Prefabs previously, we can make it accessible through the editor. When the variable is made public, we need not hardcode values such as “KeyCode.A” into the code. The code can be made flexible with as many objects as we want.

public class HexagonDestroyer : MonoBehaviour {
   
   public KeyCode keyToDestroy;

   // Update is called once per frame
   void Update () {
      
      if (Input.GetKeyDown(keyToDestroy)) {
         Destroy (gameObject);
      }
   }
}

Observe how we used the variable named “gameObject” (small g, capital O) in the method. This new gameObject variable (of type GameObject) is used to refer to the gameObject this script is attached to. If you attach this script on multiple objects, they will all react the same way whenever this variable is involved.

Do not get confused between the two, however.

  • GameObject with a capital G and O is the class that encompasses all GameObjects and provides standard methods like Instantiate, Destroy and methods to fetch components.

  • gameObject with a small g and capital O is the specific instance of a GameObject, used to refer to the gameObject this script is currently attached to.

Let us now compile our code, and head back to Unity.

Now, we will create a new hexagon sprite, and attach our script to it. Next, right-click the gameObject in the hierarchy and select Duplicate. A new sprite is created in the hierarchy; you should use the Move tool to reposition it. Repeat the steps to create similar hexagons.

Hexagon sprite Creation

Move Tool Hexagon Creation

Click on each of the hexagons and look at their script components. You can now set the individual keys so that a GameObject destroys itself when that key is pressed. For example, let us create 5 hexagons, and set them to destroy when the A, S, D, F and G keys are pressed.

You can set the same key on multiple hexagons, and they will all destroy themselves simultaneously when the key is pressed; this is an example of the use of the gameObject reference, which you can use to refer to individual objects using the script without having to set them individually.

The same key can be set on multiple hexagons, and they will all destroy themselves simultaneously when the key is pressed; this is an example of the use of the gameObject reference, which you can use to refer to individual objects using the script without having to set them individually.

It is important to understand that destroying a GameObject does not mean an object will shatter or explode. Destroying an object will simply (and immediately) cease its existence as far as the game (and its code) is concerned. The links to this object and its references are now broken, and trying to access or use either of them will usually result in errors and crashes.

Advertisements