Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by mon10lur49 · Oct 10, 2021 at 03:12 PM · tagname

Why is tag fast?

I heard that FindGameObjectWithTag is faster than GameObject.Find. But why? Although they both are strings, why are there performance differences in them?

Does tag work differently than name? I want to know why FindGameObjectWithTag is faster than GameObject.Find.

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
3

Answer by SmoothieTrash · Oct 10, 2021 at 05:52 PM

Hi @mon10lur49
Because not all gameobjects have a tag by default(they have Untagged as their tag)
alt text

therefore:
number of gameobjects with tag <= number of gameobjects
so it's faster


tag-dropdown.png (18.5 kB)
Comment
Add comment · Show 2 · 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 mon10lur49 · Oct 11, 2021 at 08:21 AM 1
Share

What you say is gameobjects with"Untagged" don't have tag variable as field? Then how does it work that comparing tag with gameobject with Untagged? Does Untagged means not only empty string, but also something?

avatar image SmoothieTrash mon10lur49 · Oct 11, 2021 at 09:46 AM 1
Share

They have, if you log it to console, you'll see a game object which is Untagged, prints Untagged. I guess that Unity has a list of GameObjects which stores gameobjects that don't have "Untagged" as tag value. Then when you call FindGameObjectWithTag, it does a linear search on that list.
Or maybe they have a Dictionary, with tags as keys, and List of GameObjects with that tag as their value (expect for Untagged gameobjects).
Either way I think it does a linear search.

avatar image
2

Answer by Pangamini · Oct 10, 2021 at 03:44 PM

As far as I know, tag is not a string, really. That's why there's GameObject.CompareTag method. Tag is internally converted from string to some simple value

Comment
Add comment · Show 3 · 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 SmoothieTrash · Oct 10, 2021 at 06:07 PM 0
Share

Hi @Panga$$anonymous$$i Read what @Bunny83 commented on top answer here, she explained the advantages of using CompareTag instead of ==/!= operator very well.

avatar image Pangamini SmoothieTrash · Oct 11, 2021 at 09:34 AM 1
Share

Ah, interesting. I thought it's because it's hashed, sort of like when dealing with animator or shader properties. But it seems like the benefit comes from the same idea

avatar image Bunny83 Pangamini · Oct 11, 2021 at 12:38 PM 1
Share

Right, we don't know the exact innerworkings. Since in order to use a tag it has to be defined in the tag manager could suggest that they actually do an index mapping of some sort. Though the other question was mainly about the performance difference between reading the tag property and using CompareTag.

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

127 People are following this question.

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

Related Questions

Calling prefabs by name or tag returns null. 1 Answer

Why isn't hit.transform.name with ray working? 1 Answer

Find a child with a name, how to?? 5 Answers

What is the component name above transform? 1 Answer

How to call a child by tag or name 1 Answer


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