I’m trying to destroy some objects as follows:
public void ClearAllEditableObjects() {
foreach (GameObject obj in GameObject.FindGameObjectsWithTag("EditableObject")) {
print ("ClearAllEditableObjects destroying object (id " +
obj.GetInstanceID() + ") with " +
((obj.rigidbody == null) ? " no rigidbody" : " a rigidbody"));
Destroy(obj);
print ("ClearAllEditableObjects testing object (id " +
obj.GetInstanceID() + ") null test yields " + (obj == null));
}
}
The text output is:
ClearAllEditableObjects destroying object (id 10262) with a rigidbody
ClearAllEditableObjects testing object (id 10262) null test yields False
My understanding is that Destroy(obj) will not destroy the object immediately, but will do so before the next frame; and that it will test as being equal to null until that happens. Any clue why this doesn’t seem to happen here? Another script is trying to use the object later (it is being returned from a Physics.OverlapSphere() call), which then causes problems. Any thoughts gratefully received! Regards,
Andy