Best way to know if an gameobject is destroyed

I’m creating a GO and adding it to a list, and then setting the GO to destroy after 10 seconds.

Thing is how do I know when the object has been destroyed so I can remove it from the list?

I don’t particularly want to put a script on the GO just to test for when it’s destroyed, but if that’s the best way then I’ll do that.

I think the only not complicated way to do it would to just be use OnDestroy() on a script on the gameObject.

if(go == null)
//go is destroyed

Unity does some magic behind the scenes, so an object that is destroyed will immediately return true on a null-check. It’s not actually quite null yet (Object.ReferenceEquals(go, null) will still be false, as the go is still in memory), but you usually don’t have to worry about that. Just check for null and you’re good to go.

You can not remove a destroyed GO from the list… it is destroyed. Because it wasn’t removed from the list beforehand, other scripts accessing this list will cause problems. You should remove the GO from the list first and then destroy it with:


if(!go) print(“go is destroyed or null”);