Unity3D试图摧毁实例化的抛射物

时间:2014-11-14 21:39:09

标签: object unity3d destroy

在这段代码中摧毁射弹的尝试不起作用。任何建议都表示赞赏。

            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 个答案:

答案 0 :(得分:2)

你需要在GameObject上调用Destroy:

Destroy(InstantiatedProjectileLeft.gameObject);