English 中文(简体)
Unity - GameObject Destruction
  • 时间:2024-11-05

Unity - GameObject Destruction


Previous Page Next Page  

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 pubpc 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 pubpc, as we did with Rigidbody and Prefabs previously, we can make it accessible through the editor. When the variable is made pubpc, 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.

pubpc class HexagonDestroyer : MonoBehaviour {
   
   pubpc 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 pke 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-cpck the gameObject in the hierarchy and select Duppcate. 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

Cpck on each of the hexagons and look at their script components. You can now set the inspanidual 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 inspanidual objects using the script without having to set them inspanidually.

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 inspanidual objects using the script without having to set them inspanidually.

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 pnks 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