- Home /
How to destroy bullets?
So,I know many people have already asked this question already, but I haven't found any good answers. I have a gun object attached to my player, and am trying to destroy the bullets after they reach a certain point. But after some point, it gives me an error :
MissingReferenceException: The object of type 'GameObject' 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. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) JoystickShoot.fire () (at Assets/JoystickShoot.js:57) JoystickShoot.Update () (at Assets/JoystickShoot.js:34)
I dont know why it gives me the error. The prefab is set to the variable bullet, which is being instantiated. Here is the destroying code:
if(this.transform.position.x > 10) {
Destroy(this.gameObject);
}
if(this.transform.position.z > 10){
Destroy(this.gameObject);
}
if(this.transform.position.x < -10){
Destroy(this.gameObject);
}
if(this.transform.position.z < -10){
Destroy(this.gameObject);
}
Please Help!
Not sure if you actually need a gameObject for a script to still be active, so perhaps, one of the if checks destroys the gameObject while another if tries to do the same later, which obviously can't be done if it already has been destroyed before. (as in - the gameObject which has the script attached already destroyed the gameObject, but the script is still running)
so perhaps you have to also destroy the instance of the script?
Have you tried putting Debug.Log(...) messages after each Destroy and check if some of them happen to fire more than once for 1 object ?
how would I go about detroying the instance of the script?
Can you post the entire JoystickShoot.js script? I don't think the error is the one ShadoX indicated. If a script is a component on a game object, the script dies with the object.
Well if you attach a small script to the prefab bullet you are instantiating, and after a certain duration you could destroy the object itself with the script attached to the bullet gameobject. It would make it easy to on a time base as compared to a distance base thing.
Answer by RyanZimmerman87 · Mar 24, 2014 at 11:47 PM
So I just realized one last detail after I typed all this other stuff below which might be worthless.
You are only using one bullet GameObject variable. And you are instantiating the bullet from itself. I think you should try using two gameObject variables for this process.
Use one to hold the initial bullet prefab, this one could easily just be a public variable so you can set it super easy to the right prefab in the editor.
And then you can create a newBullet variable for all your instantiated bullets. That's the way I've always done it for anything like bullets and projectiles.
So for example:
//set this to your bullet prefab in Editor
public GameObject bulletPrefab;
//then in your fire() function
//create a new bullet
void fireFunction()
{
GameObject newBullet;
newBullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
}
That would eliminate the possibility of having any kinds of conflicts with your variables. Right now you may be trying to instantiate or destroy the wrong bullets since you only have one bullet variable in your entire script. You are replacing the original with the instantiated one so things may get messy.
I've always used 2 GameObject variables for this kind of cloning for projectiles so I'm pretty sure you can fix it that way pretty easily.
Here's my original answer ideas before I realized what I wrote above:
So is everything working except it's just giving you that exception error?
You might wanna try using:
else if ()
After your first condition check instead of having them all be if()
If each bullet has that script on them within Update() I don't see what could be the problem unless somehow more than one:
If()
Check is executing before the deletion occurs. Maybe some editor lag could cause this if that's even possible? I was under the impression using Destroy() would immediately occur so reaching the next if() statement would be impossible.
I'm assuming you have the script in your original post attached correctly for reach bullet prefab that you instantiate and it's in the Update() function.
Answer by SkaredCreations · Mar 24, 2014 at 12:20 AM
Are you sure that you are instantiating the bullet from a prefab (in your project folders) and not from a GameObject living in the scene (that is being destroyed)? The error stack trace looks like you're trying to instantiate a GameObject that has been destroyed.
Can you add to your topic the content of the functions JoystickShoot.fire and JoystickShoot.Update? This is most likely a game logic issue.
Your answer