Unity3D attempt to destroy instantiated projectile

587 Views Asked by At

The attempt to destroy the projectiles in this snippet of code doesn't work. Any suggestions are appreciated.

            Rigidbody InstantiateedProjectileLeft = Instantiate(cannonAmmo, firingPointLeft.transform.position, firingPointLeft.transform.rotation) as Rigidbody;
            if (InstantiateedProjectileLeft != null)
            {
                //print ("Firing projectile");
                InstantiateedProjectileLeft.transform.Translate(Vector3.forward);
                InstantiateedProjectileLeft.rigidbody.AddForce(transform.forward * cannonAmmoSpeed);
                //print (InstantiateedProjectileLeft.transform.position.y);
                print ("Destroying left projectile");
                //InstantiateedProjectileLeft.renderer.material.color = Color.clear;

                Destroy(InstantiateedProjectileLeft, 1.0f); // Doesn't work
            }

            Rigidbody InstantiateedProjectileRight = Instantiate(cannonAmmo, firingPointRight.transform.position, firingPointRight.transform.rotation) as Rigidbody;
            if (InstantiateedProjectileRight != null)
            {
                //print ("Firing projectile");
                InstantiateedProjectileRight.transform.Translate(Vector3.forward);
                InstantiateedProjectileRight.rigidbody.AddForce(transform.forward * cannonAmmoSpeed);

                Destroy(InstantiateedProjectileRight, 1.0f); // Doesn't work
            }
1

There are 1 best solutions below

1
On BEST ANSWER

You need to call Destroy on the GameObject:

Destroy(InstantiatedProjectileLeft.gameObject);