How do I destroy a gun bullet clone?

Hey everyone! I know for a fact this question has been asked before; however, the other forum pages I looked at didn’t have the answer I was looking for so here goes!

Just like the topic, I’ve got a perfectly fine gun script that shoots without any problems. Unfortunately, I’m still pretty new to scripting and Unity and I’ve been having problems understanding why my bullet clone isn’t being destroyed.

using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour {

public Rigidbody rocket;
public Rigidbody n_rocket;
public float speed = 10f;
public float destroySpeed = .5f;

void FireRocket ()
{
    Rigidbody n_rocket = (Rigidbody)Instantiate(rocket, transform.position, transform.rotation);
    n_rocket.velocity = transform.forward * speed;
}

void Update ()
{
    if (Input.GetButtonDown("Fire1"))
    {
        FireRocket();   
    }
    Destroy(n_rocket, destroySpeed);
}

}

Now it works perfectly fine if I write the Destroy script like so:

Destroy (GameObject.Find(“Bullet (Clone)”), destroySpeed);

however, from what I’ve seen written on the forums, using GameObject.Find can be inefficient for something like this. So why does the GameObject.Find work but it doesn’t destroy the object if I reference it by it’s name, n_rocket?

You declare a reference to the instantiated rigidbody called “n_rocket”. Later, you use the Destroy function to destroy the referenced object, which is still a rigidbody, so only the rigidbody component of the gameobject is destroyed, not the whole GameObject.

The solution is of course to not instantiate a rigidbody, but rather a whole GameObject (which has a rigidbody you can access via GetComponent), and destroy this instead.

Edit: Or do it like this:

Destroy(n_rocket.gameObject, destroySpeed);

@Cherno is right. Also you can use n_rocket.gameObject when destroying which references to object not component.

My personal method would be just to use a coroutine.

 void FireRocket ()
 {
     Rigidbody n_rocket = (Rigidbody)Instantiate(rocket, transform.position, transform.rotation);
     n_rocket.velocity = transform.forward * speed;
     StartCoroutine(DestroyLater(n_rocket.gameObject, destroySpeed);
 }

    private IEnumerator DestroyLater(GameObject obj, float delay)
    {
        yield return new WaitForSeconds(delay);
        Destroy(obj);
    }