- Home /
Bullets dont delete properly
the bullets spawn and launch properly but the problem is that i have a destroy object script for 5 second which it works, after 5 seconds of the bullet being spwned it dletes itself the problem is that when it deletes the final bullet that spawned unless i keep firing, it wont spawn anymore bullets saying that Object type rigid body has been destroyed but im still trying to access it. how do i fix it hers a video :http://www.youtube.com/watch?v=ZuhAnaPowVs&feature=youtu.be
and my spawn script
#pragma strict
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
function Start () {
}
function Update ()
{
if(Input.GetButtonDown("Fire1")){
Fire();
}
}
function Fire(){
Bullet = Instantiate(Bullet,Spawn.position,Spawn.rotation);
Bullet.AddForce(Bullet.transform.forward*BulletSpeed);
audio.Play();
}
then delete script
Destroy (gameObject, 5);
Answer by ncallaway · Jan 06, 2014 at 01:50 AM
The problem is that when you instantiate a new bullet, you store it over the variable that you use to instantiate the next bullet.
What this means is that when the last bullet is destroyed, your Bullet variable now refers to a destroyed rigidbody. When you next attempt to instantiate a bullet, you pass Instantiate() destroyed object. To fix this, you'll want to have a separate variable that stores the newly instantiated bullet.
function Fire(){
var newBullet : Rigidbody = Instantiate(Bullet, Spawn.position, Spawn.rotation);
newBullet.AddForce(Bullet.transform.forward * BulletSpeed);
audio.Play();
}
omg it works thanks for the in depth reasoning to, actually it works but when i rotate my charcter from the original position ie look to the left the bullet comes out the gun going sideways trying to go in the same direction what the problem?
No problem! If it works, you might consider marking the answer as the accepted answer.
Accepting answers makes it easier to get your future questions answered. :)
when i rotate my charcter from the original position ie look to the left the bullet comes out the gun going sideways trying to go in the same direction what the problem?
You are using the Spawn variable to deter$$anonymous$$e each fired bullet's position and rotation. Whatever the rotation of "Spawn" is at the time of the Instantiate() call will be the rotation of the new bullet.
Without knowing more about your project, I can't really help debug why Spawn has the incorrect rotation. If I had to hazard a guess, I would say that Spawn should be a child of your character object so that when the character rotates, Spawn automatically rotates.
It's kinda a different question though, and the comments aren't really the appropriate forum for more questions, so if that doesn't get you to an answer, I'd recommend creating a new question...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem doing a script. 2 Answers
Script suddenly stopped working properly 1 Answer
Animation + Scripting = I Don't Understand (Help) 0 Answers
Bullet Hole not inline with sight?(Center of Screen) 1 Answer