FindGameObjectsWithTag returns results only within hierarchy of calling gameObject
I have the following simple validation to check if an UI pane is present in the scene:
public static bool IsMenuDialogActive()
{
return GameObject.FindGameObjectsWithTag("MenuDialog").Length > 0;
}
And i have two UI GameObjects that call that method, 'A' and 'B', and B is a child of A. 'A' is an object that can have one or more 'MenuDialog' child objects active at a time.
If I call IsMenuDialogActive() from within 'A' it returns true if any of these child objects is active. But if call it from within 'B' it returns zero objects. I suspect that FindGameObjectsWithTag only searches for tagged objects within the hierarchy of the object that calls it. But this is not clear from the documentation for FindGameObjectsWithTag. Is this a correct assumption?
Answer by tormentoarmagedoom · Mar 11, 2019 at 11:34 AM
Good day.
"Is this a correct assumption?"
No.
FindGameObjectsWithTag fins all ACTIVE objects in the scene with that tag, does not care parenting or distances, or type of object.
Bye :D! !
PS: If you create an array with this function, it will store all objects that are active in that moment. If then you setActive(false) some of the objects, it will continue inside the array.
Yeah I was wrong. After further testing I found I was deactivating all "$$anonymous$$enuDialog" objects before making the call to FindGameObjectsWithTag. Then I would immediately reactivate them after. Therefore I was not being able to notice their absence.
Your answer
![](https://koobas.hobune.stream/wayback/20220612193332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is this a bug? I can't seem to fix it! 0 Answers
Soccer game, more realistic net 1 Answer
How can i choose object with mouse ? 0 Answers
OnMouseDown function not working on a gameObject 1 Answer
Problem with destroying a gameobject 2 Answers