Differences between Using Tag and Name

This question goes more to video 43 - Switch Statements but I could not find the page for it.

Anyways, I was wondering what the difference between using gameObject.tag and gameObject.name is. Is one more optimal or does it just depend on the use case?

Hi,

Welcome to our community! :slight_smile:

What page are you looking for? Since we didn’t invent switches, you could look them up everywhere. This is one of my favourite sources of information regarding C#:

https://www.dotnetperls.com/switch

Or do you mean that a link in the resources of the videos is broken or missing?

Regarding your second question, gameObject.name refers to the name of the game object, e.g. Enemy 1, Player, Tree 99, and so on. gameObject.tag refers to the tag. Ideally, we treat tags like categories, not like normal names. Enemy 1 and Enemy 2 could, for example, have the “Enemy” tag. Unless we are looking for a specific enemy, e.g. Enemy 21, checking the “Enemy” tag would be sufficient in our game because we do not want to check in a loop if the other game objects name is “Enemy 1”, “Enemy 2”, …, “Enemy 999”. As you can probably imagine, if we have 1000 enemies, that would be a waste of resources if we are just interested in the “type/category”.


See also:

Sorry for the confusion. I meant that when I tried clicking on “This Lecture’s Discussion” for #42 and #43 (https://community.gamedev.tv/tag/14_PB_UY3) I was getting a “Page does not exist”

So just so I understand, in general, looking at gameObject.name could be more resource heavy as it searches through all objects for that name compared to gameObject.tag.

gameObject.name refers to the name property of the GameObject object assigned to gameObject. We do not look for anything here. FindObjectsOfType() would be a resource-intensive method call. Iterating over the returned array to check the name property of the respective GameObject object is not.

However, if you had 1000 enemies with 1000 different tags, you would have not only 1000 enemies which you have to look for but you’d also have 1000 string objects. There is no list with tags which keeps track of all game objects with a specific tag. While so-called primitive types such as int, float, string and so on are very performant, this whole operation (search for objects, compare stuff) could be a waste of resources. And a “waste of resource” is defined as “there are better solutions which take up less resources” or “we don’t need this at all”. It’s not “wrong” per se to call FindObjectsOfType or to use tags, you just have to evaluate if they are a good solution within your current context.

Regarding the broken links to the discussions, I’m forwarding the problem to our team. Thank you for letting us know about this issue.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms