My question on this is regarding to the nature of how gameobject.find() loops through the hierarchy.
Does it
loop through in the order that is visually presented in the editor?
Alphabetically/reverse-alpha?
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
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:”
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.