Answers for "Unity Destroy gameObject upon collision"

C#
1

unity3d on collision destroy object

Here's both ways to destroy the gameObject, or the gameObject you collide with.
void OnTriggerEnter(Collider col){
Destroy(col. gameObject);
}
void OnTriggerEnter(){
Destroy(gameObject);
}
Posted by: Guest on May-11-2021
2

unity destroy object on collision

void OnCollisionEnter2D(Collision2D coll)
}
   // Check if we have collided with the enemy
   if (coll.gameObject.tag == "Enemy")
   {
      Destroy(coll.gameObject);     
   }
}
Posted by: Guest on March-06-2021
0

Unity Destroy gameObject upon collision

void OnCollisionEnter2D(Collision2D collision)
{
    Destroy(gameOnject);
}

// Or if you only want it to be destroyed when 
// hitting something specific

void OnCollisionEnter2D(Collision2D collision)
{
    if(collision.gameObject.tag == "tag")
    {
         Destroy(gameObject);
    }
}
Posted by: Guest on September-30-2021

C# Answers by Framework

Browse Popular Code Answers by Language