It should be possible to have more than one tag on a object i think. :-)! What do you think?
Why?
It could be useful for things like grouping. But itās not too hard to put in similar logic yourself.
For instance, letās say you have three tagged types:
zombie
orc
monster
You might want them to act as a group, namely an enemy group. So if you collide with one of them, you can treat them all like an enemy.
This would actually be quite usefulā¦
This object is a Player, but heās also a Character, and heās also Team Blue, and heās also Wounded.
Quite frankly because thatās how tags work in every other place the word ātagā is used - in the rest of the world, and even within Unity! Youāre SUPPOSED to include a bunch of tags, just slap tags on things, anything and everything that applies to that object.
The fact that you can only have one tag on an object is THE reason I donāt use them. Iām always worried that if I use tags to denote that this thing is a Player, then I wonāt later be able to use tags to denote team red and team blue instead.
So instead, I use MonoBehaviours to ātagā things. Even, sometimes, empty MonoBehaviours - just to mark that this object is what it is. I can check for one of these with GetComponent<> and GetComponentInChildren<>, I can find them in a heirarchy with GetComponentsInChildren<>, and I can get a list of all of them in a scene with FindObjectsOfType<>. In other words, I use blank MonoBehaviour scripts the way we OUGHT to be able to use tags.
(Before you jump down my throat, yes of course those methods are all slow, and of course I cache them all when I use them. The point here is that I should be able to to do all of those things with tags, which if I understand correctly, have some sort of optimizations applied to them that would make such methods much faster than the ones I use.)
I simply attach an empty game object with the tag I want to use and get the parent object.
I have to admit Iāve never really used tags and in my latest project (released mobile game) I think Iāve only tagged 3 or 4 objects, and theyāre all cameras.
But I might be missing a trick here - can someone explain the benefits of extensively tagging objects?
I usually use a list or dictionary to manage groups of things.
Iāve also never used the tag system, a big reason for this being the inability to add multiple tags to an object. I think this is the main reason why you would use a tag system anyways, and if it misses that feature, I deem it pretty useless to me
There really is no need to have multiple tags. What you really could do is give every character in your game one ācharacterā tag. Then you could have a team script attached to all of them. Once you have gathered all your characters into an array, perform a for loop and get the component of the team script. Within the team script you could categorize what team they are on, what class and so forth. From there add and sort them into different arrays if needed⦠The possibilities are endless.
Itās simple, in this case, a CharacterManager gameobject and component attached to it with methods that finds all the characters on the scene using FindGameObjectsWithTag, then the characters gameobjects should have a CharacterController component with all the properties you want them to have (like health and team) and it should have be tagged Character, then the CharacterManager will have the work of find all of them and do whatever you want to do with them using for loops to add them in different lists. Like @brilliantgames said, the possibilities are endless, fo real!!
Thereās no need for many features. There is, however, quite a bit of utility.
The fact that you could only have 1 tag has always confused me, too. Itās like a name, but less useful. I assume thereās some optimization involved because itās not a text search, but still⦠The word ātagā in regards to programming has come to mean that you can attach multiple of them. Unityās is not really a tag. Itās a type, or a class, or many other words, but not ātagā.
If they keep using this terminology, people will keep asking for the obvious features that go with the term.
I like the workaround of adding tagged empties to it and just getting their parents.
Iāve run into that issue several times, where I wanted multiple tags.
I use 2 solutions to help me solve this issue.
-
by using layers instead, and detecting objects on layers, and moving things on layers,
can pretty much do the same things as tagging but requires some extra coding. -
by adding my own tag script to the gameObject, with that i can detect if the script is attached and then detect
what public bools i have enabled on that gameObject. It also requires more coding.
Works, but Is sometimes a pain.
Youāre technically correct in that you donāt need to have multiple tags, because you can avoid the ātagā system entirely. Glad we can agree that tags are useless.
no.
There are plenty of alternatives without using the tag system.
If you need multiple tags, you need to re-evaluate how youāre coding, because youāre doing it wrong.
An interesting point. You could get by and not use it at all. But I wouldnāt call it useless, as itās a bit easier than writing your own component to do it.
Multiple tags would sure be useful and the easiest way for unity to implement in my opinion would be to make the tag list similar to the static list, with checkmarks and add a function like gameObject.ContainsTag(string tag); and gameObject.tag could become gameObject.tags, a string array, or maybe a string list that would even provide the .Contains(string item) function itself and also add the possibility to add/remove tags at runtime easily.
Sure itās possible to have everything working somehow like it currently is, depending on the project you may not even need tags at all.
But personally iāve ran into some cases where it would have been pleasent to give a gameObject multiple tags.
i need this for my interaction system
lets say i have a candle game object and i want to light itā¦
I will set the tag Interactable but then i want to make a system to count how many candles remain to light up
so it would be easy to add a tag āCandleā and a tag āInteractableā to make my job easier
Itās already possible: create and use MonoBehaviours such as TagCandle
and TagInteractable
.
Thatās the cheese way. You could also use interfaces. You donāt have to reach for actual Unity Tags at all, and I actually would NOT, since they provide nearly zero value and tie your code to the project.
Using Interfaces in Unity3D:
https://forum.unity.com/threads/manager-classes.965609/#post-6286820
Check Youtube for other tutorials about interfaces and working in Unity3D. Itās a pretty powerful combination.