CannonBall Collision

So I have an animated cannonball, which i want to hit and wall, and then the wall to be destroyed.
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == “CannonBall”)
{
Destroy(collision.gameObject);
}
}

This is what i have, but i want to destroy the wall the cannon ball hits and the cannon ball.
?

First off, that code is indeed in a script attached to the wall right? I would do it the other way around (attach the script to the cannon ball, which then finds a “wall” tag on collision), but it doesn’t matter too much.

The way you have it written, the wall will destroy the cannonball because collision.gameObject is the object that is colliding (the cannonball) with the object in your script (the wall). If you want to destroy the wall you have to do:

Destroy(collision.contacts[0].thisCollider.gameObject);

Since you want to destroy both, include your previous Destroy statement as well.

Make sure your wall isn’t touching any other objects otherwise you’ll need to expand this to:

function OnCollisionEnter(collision : Collision)
     {
     for (var contact : ContactPoint in collision.contacts)
          {
          if (collision.gameObject.tag == "CannonBall")
               {
               Destroy(contact.thisCollider.gameObject);
               Destroy(contact.otherCollider.gameObject);
               }
          }
    }