How do I check this? Is there a way to do this? It may seems like a dumb question (it may be) but when working with prefabs nothing works, activeInHierarchy, the GameObject bool operator, nothing works because in fact the object exists because of the prefab, checking those will return true but they are not strict to the scene hierarchy, it can be “active in hierarchy” but not on the scene…
As a way,
you can assing specific tag to you prefab,
GameObject.FindWithTag("YourTag"). This function will return one active GameObject with this tag, if exist
Like this? You could also change the string parameter to GameObject and change the if condition accordingly.
public bool IsChildAliveInParent(Transform parent, string childToCheck)
bool isChildAlive = false;
for(int i = 0; i < parent.childCount; i++)
isChildAlive = true;
It seems like I need to null the reference to the object that I’m checking if it exists, even when the object gets destroyed because you can still access the object after it gets destroyed and if I null check it it will return false (in the inspector it will show as Missing, the reference is not null which is pseudo not destroyed in this case, but I destroyed it so I want it to be null), so I needed to make sure the reference is manually nulled after the object got destroyed.
BTW, this weirdness only happens in the same frame that the object gets destroyed, need to wait a frame before null comparing the object, if you wait you will not get not null for the object destroyed. This is needed for OnDestroy do its thing.
Checking if objects are active and spawned in the Hierarchy (if you have a field that can contains prefabs and spawned objects) I don’t know if this is possible.
I just stumbled onto this again: