Hi,
I have an arraylist of objects - they’re all instances of monobehaviour-based classes, that all implement an interface. Every now and then, I need to loop through this array, and do something with these objects. The problem is, sometimes one of the gameobjects corresponding to these objects has been destroyed, so I’m getting an error that tells me the object of type X has been destroyed, and to either check if the object is null, or not to destroy it.
The problem is, if I check if the object is null, I get false. I can Debug.Log the object and still get the name of the class it belongs to.
But if I check if object.gameObject == null, I get the same error telling me that the object of type X has been destroyed.
To clarify (untested pseudo-code) :
for(int i=0;i<objects.Count;i++) {
if(objects[i]!=null) {
// this returns true
if(((MonoBehaviour)objects[i]).gameObject!=null) {
// this throws a MissingReferenceException
// do stuff
}
}
}
Any ideas on how to solve this? It is not an option to remove the object from the arraylist manually when it gets destroyed.
Thanks!