- Home /
Which is faster, GameObject.Find or GameObject.FindWIthTag?
I have a master 'controller' object in the scene which many objects need reference to. Would tagging the object and finding it be faster than using find with object's name?
Answer by Silverfell-2 · Apr 19, 2010 at 08:07 AM
Based on this line:
For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag.
From: http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
I would say that finding by tag is far less resource intensive.
does the same go for components? or variables on others objects? or should those be global variables? Thanks in advance!
Would also be interested in knowing how Find and FindWithTag compare to using global variables, both on a speed and memory level? What's best?
Don't compare Find / FindWithTag with "global variables". It's something completely different. FindWithTag is probably faster than Find since, as far as i know, Unity use seperate arrays for each tag. GetComponent is usually faster than those Find methods since it only have to search on a given GameObject, but it also has a totally different usage. GetComponentsInChildren is of course worse but it depends on how big and deep your hierarchy is downwards.
The general rule is: Avoid calling any of those searching functions every frame. GetComponent is not that bad, but in most cases it's not difficult to get the reference once and then just use the cached reference.
When you say "global variables" i guess you talk about static varaibles? As i said above, that's something totally different. Static variables belong to the class, not to an instance and Find / Getcomponent / ... are all about instances.
The "speed level" can simply be tested, but it's obvious that a direct reference is always faster than using a function that searches for an object (especially when it has to use a string compare). I'm not sure what you mean by "memory level" and what you mean by "best". You have to give two concrete samples which you want to compare. This is by far to general
Great explanation, that's making much more sense now :)
What I meant by 'global variables' is, for example, if we make a GameObject public inside a script and then drag the object from the scene into its slot in the Inspector. I was wondering what the difference is between having a constant reference like this compared to a cached one using Find, and in what circumstance to use each.
The question about memory refers more to this, in that whether the 'global variable' object is stored in memory throughout?
Answer by dwalker350 · Aug 12, 2015 at 06:01 AM
Be warned that FindWithTag is NOT guaranteed to find only a single object - even if you've only set a single object in the editor with that tag. A bug still in the current (5.1.2) version will occasionally create invisible clones with the same tag, and "(Clone)" appended to the name. I'd recommend checking the name of the result(s) returned from FindWithTag.
@dwalker350 I sometimes encounter a weird null pointer exception when executing command for an array of objects found with GameObject.FindGameObjectsWithTag method.
It happens randomly. I have 5.3 version. Could this be a result of the bug you described?
I use unity 5.4 and it has bugs that FindWithTag returns GameObject that is the prefab! So FindWithTag not only returns Active game objects, but also returns non-instantiated prefab from the asset folder if you happen to attach the prefab to some game object's field.
@off99555 What do you mean by "attach the prefab to some game object's field" ?
Do you mean if you like have an active game object, and the non-instantiated prefab from your asset folder, is set in a field in a class on that gameobject ? That it's then returned in the FindWithTag-search function ?
Yes. Suppose that you have a Game Controller object and it has Game Controller script attach to it. The script has a field GameObject obj and you attach the prefab into this obj slot via Unity editor. And you have Player object, with Player script, inside the script you call FindWithTag(tag) with the tag of the prefab. Here, the 2 scripts are not related but the problem still persist.
Your answer
Follow this Question
Related Questions
How to get the objects inside another object and put them in a array? 1 Answer
Whats the best workaround for finding Gameobjects at Start? 1 Answer
transform.find and Instantiate do not return the same type? 1 Answer
Alternatives to GameObject.Find(); 1 Answer
Finding GameObjects within a radius? 2 Answers