Destroying Elements From An Array

Hello all,

I am trying to cycle through all of my bullets making sure they are can still be seen by the user. If not I want to delete them. However, I get this error when I try to delete a bullet:

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.
GameManager.Update () (at Assets/Scripts/GameManager.cs:38)

I can’t figure out this error because I am already checking to make sure that the bullet isn’t null. Here is the code that handles the bullet deletion:

foreach (GameObject bullet in Bullets) {

	if (bullet.transform.position.y >= cameraBounds.y && bullet != null) {

		Destroy(bullet);
	}
}

Any help would be greatly appreciated.

You check the bullets transform before you check if the bullet is null. Hence the null reference error. Try the following:

foreach (GameObject bullet in Bullets) {
    if (bullet != null && bullet.transform.position.y >= cameraBounds.y) {
         Destroy(bullet);
    }
}