How does gameobject.find() loop through the hierarchy?

My question on this is regarding to the nature of how gameobject.find() loops through the hierarchy.

Does it

  1. loop through in the order that is visually presented in the editor?
  2. Alphabetically/reverse-alpha?
  3. some other method (BST maybe?) ?

I’m asking because I want to find the most efficient way to find a gameobject when i know it exists and is a unique object and will be one only (if not then at most 1 of 10) game object referenced with find() in the scene.

My thought process: If unity does the loop alphabetically, then would having an object named “_restartMarker” then be O1 (constant time) for find(“_restartMarker”) because it should be the first item checked?

My point also runs on the assumption that Unity will break the loop when it finds the first instance of the object, which, if this isn’t true then my question is kaput. In that case, is finding an object by tag then the most efficient way to find a game object? I’m staying away from using a "Public Gameobject Target " assignment in code

Default Hierarchy Sorting is Transform Sorting.

Every gameObject directly in hierarchy is root. All roots are siblings to themseves. (RED ONE).

Every child of root (or more nested gO) is a sibling to other children in that gO(BLUE and GREEN).

In Transform class we have methods like:

SetSiblingIndex(), GetSiblingIndex() and other binded with siblings and roots. That how this sorting work.

GameObject.Find() start searching in hierarchy by sibling index.

Now we searching something like gameObject with name “unity”:

  1. Main Camera … - have no children so we go to FirstRoot
  2. FirstRoot - he have children so now we searching in chldren
  3. Zero Child
  4. FirstChild - we don’t find what we want so we back to next root
  5. SecondRoot - we avoided Second Child because it was inactive
  6. Zero Child (in SecondRoot) and as long as we find what we want in hierarchy.

If we find what we want - we stop searching!

Where is a problem?

If we have 1k gameObject and we searching for the last one - the “1000” - first we must check first 999 gameobject to find the “1000” one.

If we put “1000” at beging of hierarchy we find this gameObject instantly.

Thats why when we have bigger hierarchy we spent more time!

The documentation says that it iterates all GameObjects.

“It is a general best practice to
eliminate all usage of Object.Find and
Object.FindObjectOfType in production
code. As these APIs require Unity to
iterate over all GameObjects and
Components in memory, they rapidly
become non-performant as the scope of
a project grows.”

If you are looking for unique GameObjects, you should use the singleton patern as the documentation says:

“An exception to the above rule can be
made in accessors for singleton
objects.
A global manager object often exposes
an “instance” property, and often has
a FindObjectOfType call in the getter
to detect pre-existing instances of
the singleton:”

class SomeSingleton {

    private SomeSingleton _instance;

    public SomeSingleton Instance {

        get {

            if(_instance == null) { 

                _instance =

                    FindObjectOfType<SomeSingleton>(); 

            }

            if(_instnace == null) { 

                _instance = CreateSomeSingleton();

            }

            return _instance;

        }

    }

}

Full info here.

Why not give it a simple script that will have a static variable of GameObject and you assign your gameObject as the value on Awake()? And ofcourse you can remove that script when you get the refernce, seems faster to me.