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 John898 · Nov 02, 2014 at 03:15 AM · objectbeginnertag

When should I identify an object by its tag rather than its name?

From what I understand I can specify an object with "other.gameObject.name" which goes off of the name given to an object, and you can you use "other.gameObject.tag" to specify an object by a given tag.

I'm not sure when I should use one or the other or what the benefit of one is.

My question is which should I use and when?

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
2

Answer by Eric5h5 · Nov 02, 2014 at 03:16 AM

Tags are used to identify groups of objects. For example, say you're making a sports game, and you have a "red" team and a "blue" team. Each player on both teams can have an individual name, but would have either a "red" tag or a "blue" tag in order to tell which team they're on.

Comment
Add comment · 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
1

Answer by Owen-Reynolds · Nov 02, 2014 at 06:39 AM

Tags are just one more way of grouping things. A way of saying these 10 gameObjects all "count as" pickups, or enemies (or red or blue teams.) Don't go out of your way to use them. Eventually, you might be writing some code and want to ask "does the thing that hit me count as being catchable?" and tags might help.

Tags don't do anything by themselves, except give your code something to read. Layers seem like tags but they can affect Physics and raycasting, so you often have to set layers a certain way. For example, ghosts are on a layer to go through walls, and orcs aren't, but you can use tags to say they're both monsters.

You never have to use tags. For example, you could have all enemies' gameObject names start with an E, and check that. But giving them all an "enemy" tag might look nicer. Or give them a script, and put type info in variables (tags hold just one piece of info -- a script can hold all you want.)

Speed-wise, don't worry. Even though tags are strings, I think they get converted into integers when using CompareTag. So a tag look-up is maybe faster than a name look-up. But that tiny possible speed isn't worth confusing code. Plus, you never do name lookups once you learn to compare transforms.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't Return the Tag of a Child Object 4 Answers

Drag Script Not Working 1 Answer

Prefab starts halfway through object when placed down 2 Answers

Move from one object to another 0 Answers

Destroy objects by clicking on them 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