The object of type 'Rigidbody' has been destroyed but you are still trying to access it.

I have a basic missile launcher set up. The collision works fine, the explosion animation plays when the missile collides with something, and then it gets destroyed. The only problem is, I can’t shoot any more missiles after the first one!

I have a prefab named “missile” with a rigidbody attached, which is associated with the projectile variable from MissileLauncher. Is there another way to instantiate a new projectile after destroying the previous gameObject?

public class MissileLauncher : MonoBehaviour
{
    public Rigidbody projectile;
    int speed = 20;
 
    void Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        { 
            projectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
            projectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity
 
            Physics.IgnoreCollision(projectile.collider, transform.root.collider); //Avoid projectile an hero
        }
    }
}

public class Projectile : MonoBehaviour
{
    public GameObject explosion;
 
    void OnCollisionEnter(Collision collision)
    {
        ContactPoint contact = collision.contacts[0];
        transform.rotation = Quaternion.FromToRotation (Vector3.up, contact.normal);
 
        explosion = (GameObject)Instantiate(explosion, contact.point, transform.rotation);
 
        Destroy(gameObject);
    }
}

public class Explosion : MonoBehaviour
{
    float explosionTime = 0.25f;
 
    void Start ()
    {
        Destroy (gameObject, explosionTime);
    }
}

Instantiate will return the object it instantiates. You overwrite your original prefab reference with your instance. When this instance get destroyed you don’t have any source object you can Instantiate.

So, just do it like this:

    // [...]
    if (Input.GetButtonDown("Fire1"))
    {
        Rigidbody clone = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection(new Vector3(0,0,speed));
        Physics.IgnoreCollision(clone.collider, transform.root.collider);
    }
    // [...]

I don’t have experiente with C# but from what I see, you are trying to acces the same object that you destroy in the Projectile script.Try this : declare a new variable to hold a reference of the instantiated projectile ( you are using the same identifier, “projectile” for the projectile prefab that you instantiate, and also for the variable that holds the reference to that new instance).

public class MissileLauncher : MonoBehaviour
{
    public Rigidbody projectile;
	public Rigidbody spawnedProjectile;
    int speed = 50;

    void Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        { 
            spawnedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
            spawnedProjectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity

            Physics.IgnoreCollision(spawnedProjectile.collider, transform.root.collider); //Avoid projectile an hero
        }
    }
}

This should work for you.