Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Extrakun · Apr 19, 2010 at 06:42 AM · gameobjectperformancefindfind-gameobject

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt 15 · Nov 16, 2010 at 01:51 PM 0
Share

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

avatar image BilboStabbins · Aug 22, 2012 at 06:19 PM 0
Share

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?

avatar image Bunny83 · Aug 22, 2012 at 06:40 PM 4
Share

@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

avatar image BilboStabbins · Aug 22, 2012 at 06:52 PM 0
Share

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?

avatar image
2

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image youblistermypaint · Mar 14, 2016 at 05:04 PM 0
Share

Is there a bug tracker link for this issue?

avatar image pdenton · Jun 01, 2016 at 11:20 AM 0
Share

@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?

avatar image off99555 · Oct 30, 2016 at 04:26 PM 0
Share

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.

avatar image Westland off99555 · Nov 01, 2016 at 11:27 AM 0
Share

@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 ?

avatar image off99555 Westland · Nov 01, 2016 at 07:26 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges