(Help) can't destroy instantiated prefab

i have instatiated a transform var, which is connected to a prefab. when i try and destroy it, it comes up with an error, "can't remove transform as mesh render, box collider and rigidbody relies on it."

heres the code. i don't understand, i should be sound.

var bullet_Prefab : Transform; var speed = 5;

function Update () {

if(Input.GetButtonDown("Fire1")) { var bullet = Instantiate(bullet_Prefab, transform.position, Quaternion.identity);

  bullet.rigidbody.AddForce(transform.forward * speed);

  Destroy(bullet, 5);

}

}

2 Answers

2

You should be using a GameObject to store your prefab, not a transform. When you're calling Destroy it is just trying to destroy the transform component, not the entire GameObject.

var bullet_Prefab : GameObject; 
var speed = 5;

function Update () {

if(Input.GetButtonDown("Fire1")) { var bullet = Instantiate(bullet_Prefab, transform.position, Quaternion.identity);

  bullet.rigidbody.AddForce(transform.forward * speed);

  Destroy(bullet, 5);
}

}

Alternativly,

Destroy(bullet.gameObject,5)

Thank You @jtbentley. THAT solved my problem. Seeing as the Unity docs themselves prefer instantiating a transform above instantiating a GameObject. http://docs.unity3d.com/Manual/InstantiatingPrefabs.html