Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

multiple tags on object

Discussion in 'Editor & General Support' started by NinjaRubberBand, May 12, 2014.

Thread Status:
Not open for further replies.
  1. NinjaRubberBand

    NinjaRubberBand

    Joined:
    Feb 22, 2013
    Posts:
    243
    It should be possible to have more than one tag on a object i think. :)! What do you think?
     
  2. Wacky-Moose

    Wacky-Moose

    Joined:
    Jun 19, 2013
    Posts:
    63
  3. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    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.
     
  4. AwesomeX

    AwesomeX

    Joined:
    Sep 10, 2013
    Posts:
    115
    This would actually be quite useful..
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    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.)
     
    Last edited: May 12, 2014
    JauCimeto likes this.
  6. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    I simply attach an empty game object with the tag I want to use and get the parent object.
     
  7. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    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?
     
  8. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    I usually use a list or dictionary to manage groups of things.
     
  9. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    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
     
  10. brilliantgames

    brilliantgames

    Joined:
    Jan 7, 2012
    Posts:
    1,937
    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.
     
  11. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    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!!
     
  12. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    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.
     
  13. Vaupell

    Vaupell

    Joined:
    Dec 2, 2013
    Posts:
    301
    I've run into that issue several times, where I wanted multiple tags.

    I use 2 solutions to help me solve this issue.

    1) 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.

    2) 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.
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    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.
     
  15. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    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.
     
    Warsymphony likes this.
  16. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    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.
     
  17. Fuzzy

    Fuzzy

    Joined:
    Feb 11, 2011
    Posts:
    266
    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.
     
  18. darkboss240

    darkboss240

    Joined:
    Apr 8, 2022
    Posts:
    1
    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
     
  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    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/how...rereceiver-error-message.920801/#post-6028457

    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.
     
Thread Status:
Not open for further replies.