How do I make a projectile destroy a game object?

Hi, I am still a student learning about how to use Unity & I have created a player that shoots a cannonball projectile, but when I shoot my game object, it isn’t destroying it. I have a beginning script that I’m using & can’t figure out what is wrong with it. Can anyone help me? Thank you in advance! Here is the script below that I’m using:

#pragma strict


var projectile : GameObject;
var fireRate : float = 0.5;
internal var nextFire : float;
var speed : float = 5;



function Update () {

   // fire1 button is left mouse or left control key
  if (Input.GetButton ("Fire1") && Time.time > nextFire) {
     nextFire = Time.time + fireRate;
    var clone :GameObject = Instantiate (projectile, transform.position, transform.rotation);
    clone.rigidbody.velocity = transform.TransformDirection (Vector3 (0,0, speed));
    Physics.IgnoreCollision(clone.collider, transform.root.collider);
    
    }
	
}

 function OnCollisionEnter(collision : Collision)
 {
     if(collision.gameObject.tag == "Gem2")
     {
         Destroy(gameObject); // destroy the object to which script is attached
             Destroy(collision.gameObject); // destroy the other collided object
     }
 }
  1. You need the OnCollisionEnter Event on your projectile and not on the Player

  2. I believe when you destroy your gameobject no further code gets executed, so you have to swap the destory calls

    if(collision.gameObject.tag == “Gem2”)
    {
    Destroy(gameObject); // destroy the object to which script is attached
    Destroy(collision.gameObject); // destroy the other collided object
    }

Change to:

if(collision.gameObject.tag == "Gem2")
{
     Destroy(collision.gameObject); // destroy the other collided object
     Destroy(gameObject); // destroy the object to which script is attached              
}