- Home /
Bullet not dissapearing after time ?
Hi ,I have a small prob people ? ,I attach this to a bullet (Sphere),and the bullets fire correctly but they just stop after 5 seconds not destroyed (They just hang there lol ) ? Any ideas please,its probably very obvious lol :)
var projectile : Rigidbody; function Update () {
if (Input.GetButtonDown("Fire1")) {
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward * 100);
Destroy(clone , 5);
}
}
Closer ! Using left mouse fires my spheres ,and right mouse deletes them all :)
function Update () {
if (Input.GetButtonDown("Fire2")) {
Destroy (gameObject);
}
}
But I cannot fire again because of this error ? :(
MissingReferenceException: The object of type 'Rigidbody' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
Answer by Eric5h5 · Jan 08, 2013 at 03:41 AM
"Destroy(clone , 5)" should be "Destroy(clone.gameObject, 5)".
Answer by youngapprentice · Jan 08, 2013 at 02:39 AM
They hang in the air because when you define clone as Rigidbody, you are saving a copy of the Rigidbody attached to that clone. Try changing it to
//Untested
if (Input.GetButtonDown("Fire1")) {
var clonePhysics : Rigidbody;
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
clonePhysics = clone.rigidBody;
clonePhysics.velocity = transform.TransformDirection (Vector3.forward * 100);
Destroy(clone , 5);
}
}
That won't work either, since you defined clone as a Transform component.
Answer by luciferlc · Jan 08, 2013 at 03:36 AM
I think you had better control the bullet appear or disappear.
Your answer
Follow this Question
Related Questions
Destroy (Clone) 1 Answer
Need help with my gun script! 1 Answer
Which is better, shooting a bullet from camera or from the gun itself? 3 Answers
Setting bullet instansiate direction? help? 1 Answer
Cannot destroy instantiated objects 2 Answers