- Home /
Question by
nuri_amari · Jul 12, 2014 at 08:20 PM ·
arrayprogrammingnullreferenceexception
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:
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) {MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
if (bullet.transform.position.y >= cameraBounds.y && bullet != null) {
Destroy(bullet); } } Any help would be greatly appreciated.
Comment
Answer by Kiwasi · Jul 12, 2014 at 08:40 PM
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);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Working Reference Still Throwing Null Reference Error 0 Answers
A node in a childnode? 1 Answer
Unity Object Array C# 2 Answers
What is the technical name of component-based approach in Unity? 1 Answer