How To Destroy An Object On Collision In Unity

what is this series about

In this series I will be discussing the process of how I made my first mobile application, Dr. Dino, giving you tips, tricks, and tutorials on how you can get started with game development.

If you want to check out the first game I ever created, click the button below:

In this post, we will discuss how to destroy an object on collision.

destroying objects on collision

Destroying other objects that collide with the main object

To destroy an object on collision within the using ty software, you have to use some form of the void OnCollisionEnter method. For 2D games you need the void OnCollisionEnter2D() method and the void OnCollisionEnter() method for 3D games.

Both of these methods will activate when the object that the script is attached to has a collider and collides with an object. When the methods are activated, the Destroy function activates. When the Destroy(this.gameObject) function is activated, the object that this script is attached to will be destroyed.

See the sample code to destroy objects on collision below.

Destroying the main object that collide with other objects

To destroy an object on collision within the using ty software, you have to use some form of the void OnCollisionEnter method. For 2D games you need the void OnCollisionEnter2D() method and the void OnCollisionEnter() method for 3D games.

Both of these methods will activate when the object that the script is attached to has a collider and collides with an object. When the methods are activated, the Destroy function activates. When the Destroy(collision.gameObject) function is activated, the object that collides with the object that this script is attached to will be destroyed.

See the sample code to destroy objects on collision below.

download our games and applications

Similar Posts