TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Unity GameObject Destruction

Unity GameObject Destruction with Introduction, Installing, GameObject, First Unity Project, Unity 2D, Sprite Unity, Loops, If Statement, Data Types, Swith Statements, Unity UI, Unity Asset Store etc.

<< Back to UNITY

Unity GameObject Destruction

Like instantiation, the destruction of GameObjects is also important. In this section, we will understand how to destroy the GameObjects.

Destroying a GameObject is very simple as creating a GameObject. You require a reference to the object to be destroyed, and call the Destroy() method with this reference as a parameter.

Let's create 5 diamonds, which destroy themselves when an assigned key is pressed.

First of all, create a new script. Rename it to Destroyer.cs and open it on an editor. Now, copy the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Destroyer : MonoBehaviour
{
     public KeyCode keyToDestroy;

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

Here, we used a public variable for KeyCode. A KeyCode is used to describe a key on a standard keyboard, and the input class in its method uses it. By making this variable public, we can make it accessible through the editor. When the variable is made public, we don't need hardcode values such as KeyCode.A into the code.

Here, we have added the gameObject variable. This new gameObject variable is used to refer to the gameObject this script is attached to. If you add this script on multiple objects, they will all react the same way whenever this variable is involved.

Now, we will create a new diamond sprite and attach our script (Destructor.cs) to it. Next, right-click on the gameObject from the hierarchy tab and select the Duplicate option. A new sprite is produced in the hierarchy tab; you should use the Move tool to reposition it. Repeat the steps to create similar diamonds.

Unity GameObject Destruction
Unity GameObject Destruction

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

Unity GameObject Destruction
Unity GameObject Destruction

The same key can be set on multiple diamonds, 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 particular objects using the script without having to set them individually.

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

Unity GameObject Destruction
Next TopicUnity Console




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf