x


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?

more ▼

asked Apr 19 '10 at 06:42 AM

Extrakun gravatar image

Extrakun
1.3k 44 56 70

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 19 '10 at 08:07 AM

Silverfell gravatar image

Silverfell
98 1 1 7

does the same go for components? or variables on others objects? or should those be global variables? Thanks in advance!

Nov 16 '10 at 01:51 PM Matt 15

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?

Aug 22 '12 at 06:19 PM BilboStabbins

@BilboStabbins:

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

Aug 22 '12 at 06:40 PM Bunny83

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?

Aug 22 '12 at 06:52 PM BilboStabbins
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2079
x661
x206
x16

asked: Apr 19 '10 at 06:42 AM

Seen: 4748 times

Last Updated: Aug 23 '12 at 03:43 PM