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!

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.

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.