GameObject.FindGameObjectsWithTag() isn't working.

I’m trying to find objects with a certain tag, but this:

GameObject[] rootElements = GameObject.FindGameObjectsWithTag("UIRootElement");

isn’t working for some reason. The objects definitely exist with the correct tag, but it just never finds anything.

Running this code:

var allObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; // grab all gameobjects
foreach (GameObject obj in allObjects) {
    if (obj.tag == "Untagged") continue;

    if (obj.CompareTag("UIRootElement"))
        Debug.LogError(obj.name);
}

finds my 2 GameObjects with the correct tag, but I don’t think this is a very good solution and I’d like to know why GameObject.FindGameObjectsWithTag isn’t working.

Hi, I’m not on my workstation at the moment so I can’t test that, but are your tagged GameObjects active? Inactive objects will not show up in find results.

1 Like

Yes, they are active.

Can you show the objects in the scene that you are trying to find? Please show their position in the hierarchy as well as the inspector window.