How to Destroy a GameObject on Collision in Unity.

There are many reasons why you would want to destroy a gameobject in your app. Depending on the situation, you can use a specific function to make this happen, such as OnCollisionEnter().

To destroy a gameobject on collision, use the OnCollisionEnter() function for the collision itself and the Destroy() function to destroy a gameobject when it collides with another gameobject. At least one of the objects must have a rigidbody component, and both need to have collider components.

Everything is easier said than done, even if it’s a simple task. So let’s go through this. First, below is a code example:

public class Player : MonoBehaviour
{
   private void OnCollisionEnter(Collision collision)
   {
      Destroy(gameObject);
   }
}

Let’s explain the the above code. Before we do, however, there are a few important things to know. First, to have any collision between objects, the objects need to have a Collider Component attached to them. This collider will need to be attached to both objects for them to collide. Attach the collider in the Inspector window.

Also, one of the objects must have a Rigidbody attached.

We’ll start with the OnCollisionEnter() function. Notice the parameters in the parenthesis. “Collision” describes the collision itself. This store properties about the collision, and these properties can be used in code to access this information. For example, you can use collision.gameObject to actually retrieve the gameObject that came into contact with the gameObject that has this script attached. More on that later though.

The other vital part of this code is the Destroy() function. This function is what removes the specific gameobject from the scene. So the code above, “Destroy(gameObject)” will destroy the gameObject that this script is attached to.

How Do You Destroy Other objects in Unity?

What if you want to destroy the other gameobject that was part of the collision that does not have this script attached to it? I’m glad I asked. See the code below.

private void OnCollisionEnter(Collision collision)
{
   Destroy(collision.gameObject);
}

Not much of a difference. The only change I made was in the parameters for the Destroy function. Remember when I said that the collision parameter of the OnCollisionEnter() function stores information about what the object with this script attached collided with? It allows us to find exactly what object we collided with, and it allows us to destroy that object as well. Pretty easy. Of course, there is other information you can get from the collision that can be used for your specific needs.

Destroy a GameObject with a Tag on Collision.

There are many times that you want to destroy an object when it collides with another object depending on what type of object it is. For example, if the player touches an enemy, you may want the enemy gameobject to be destroyed. At the same time, the player may be walking on the ground, colliding with the ground’s collider component. You wouldn’t want the floor to also be destroyed by the player just because it touches it.

This is where tags are very useful.

To destroy a gameobject with a tag in a collision, select the gameobject to be destroyed in the Unity editor. Click on the “Tag” drop-down towards the top of the inspector, and select the tag you would like to use. You may need to make a tag. Finally, use OnCollisionEnter() and use a conditional statement to check if the other object that is hit has the correct tag. Then destroy that object.

if (collision.gameObject.tag == "Enemy")
{
   Destroy(collision.gameObject);
}

With the code above, if the gameObject with this script collides with a gameObject tagged as “Enemy”, the tagged gameObject will be be destroyed. No other gameObject that collides will be destroyed.

Breaking Down the Code in this Example.

Even if you are just going to copy and paste the code above into your project, if you are going to learn and understand Unity and C# then it is very important to understand the example code.

private void OnCollisionEnter(Collision collision)
{
   Destroy(collision.gameObject);
}

“private” makes the OnCollisionEnter() method inaccesible to any class other than the one the method is in.

“void” is a return type. In this case, OnCollisionEnter() is not returning any value, so void is used.

“Collision” is something that can be used to describe a collision between two objects. It holds information about what is being collided with a certain object. For example, in the code above in the Destroy() function, we are accessing the gameObject that collided with the gameObject that has this script attached because we want to destroy it.

“{}” are brackets that are used to encapsulate a code block. For example, everything in between these are part of the OnCollisiontEnter() function. If they were outside of the brackets, then they would not be part of the function.

“Destroy()” is a function that allows you to destroy or remove an object from the scene. It accesses the data from the collision and gets the gameObject involved in the collision and then removes it from the scene. You can also add another parameter to the method that allows you to wait a certain amount of time before the object is destroyed.

if (collision.gameObject.tag == "Enemy")
{
   Destroy(collision.gameObject);
}

“if ()” is a conditional statement that allows you to compare certain data in your app and then execute code based on which condition is met. The code that is executed is generally in between to curly braces. If there is only one code statement that is executed, then you do not need curly braces. However, to avoid confusion you should just use curly braces every time.

“collision.gameObject.tag == “Enemy” ” is part of the if statement. This statement is checking to the collision data, accessing the gameObject of what collided and what the tag of that object was. It then uses the comparative operate == to see if the tag of that gameObject is the same as the Enemy tag. If it is the same, then the code in between the curly braces will be executed.

“Destroy(collision.gameObject)” is the code that is executed in the if statement if the condition is met. Again, Destroy() is a function that allows you to destroy or remove an object from the scene. It accesses the data from the collision and gets the gameObject involved in the collision and then removes it from the scene.

Remember that OnCollisionEnter() is different than OnTriggerEnter(), and are used in different cases. Though they are functionally similar.

Source1, source2.

Similar Posts