How does FindGameObjectsWithTag work?

I was just curious if anyone knew specifically how GameObject.FindGameObjectsWithTag worked.

Is there some sort of dictionary that these tags and gameobjects and whatnot are a part of?

or is it the equivalent to

foreach(GameObject go in FindObjectsOfType(typeof(GameObject)))
{
    if(go.tag == "butts")
    {
        return go;
    }
}

I was just curious about how efficient it is to use if you are trying to grab a group of objects with the same tag

All of the Find functions for GameObjects [Find(), FindGameObjectsWithTag(), FindWithTag()] are notoriously inefficient. At one of the Unite 13 talks one of the programmers at Unity stated that you shouldn’t use these unless absolutely necessary. The recommendation I have heard most often is to register gameobjects to into a custom data structure that is easily and efficiently traversable.

To answer your question though, when you select an item in your scene heirarchy, on the right side (assuming std Unity setup), you’ll see at the top a “Tag” menu. You can add custom tags to that, then assign your objects to the tag.

The FindGameObjectsWithTag function should return an array of GameObjects.

        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Enemy");

Apparently, the tag is internally stored in the GameObject as an integer, probably the index of the actual tag name in the tag table - that’s why you must register a tag before using it. Since GameObject.tag is a property, its getter function returns the corresponding string stored in the tag table. I don’t know if there exists some additional mapping to speed up tag search, but comparing integers is way faster than comparing strings, what makes FindWithTag faster than Find(“tagName”).