How do I destroy a prefab with a prefab?

Hey, I’m making a 2D game, which requires destroying one prefab with another. One of the prefabs is a rigidbody, the other is just a normal gameobject. The rigidbody that hits the gameobject is called Bolt and the gameobject is called enemy1. I’ve looked around for hours and not found anything that’s worked! Cheers in advance :slight_smile:

The direct answer to your question is to have an OnCollisionEnter event for the projectile like so:

public void OnCollisionEnter(Collision event)
{
    GameObject object = event.collider.gameObject;
    GameObject.Destroy(object);
}

Your bolt can figure out from the Collision information what collider was hit, and from that, what the game object that the collider is attached to is. From there…destroy!

However, it might make more sense in terms of game rules for the collidee to self-destruct if its health/HP falls too low! Instead of calling:

GameObject.Destroy(object);

…You’d instead do something like:

object.SendMessage("TakeDamage", boltDamage, SendMessageOptions.DontRequireReceiver);

…if the entity’s HP is less than or at 0, some death effect happens, and (sooner or later) the game object is destroyed.

Hope all of that helps!