Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Feedback Multiple Tags for a game object

Discussion in '2021.1 Beta' started by FernandoMK, Nov 1, 2020.

  1. FernandoMK

    FernandoMK

    Joined:
    Feb 21, 2017
    Posts:
    178
    Working with TAGs is very practical, but each game object can only have one, why not several tags?

    It would be very practical to add more than one tag to a given game object (As we do for example with layers)

    With that, we could, for example, mark an enemy not only with the "enemy" tag, but also with the "archer" tag and so on... a way to nest the tags
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    You can use empty components for that instead, way less error-prone (no chance to wrongly type the component name) and as a bonus you can easily search in the hierarchy for objects with the given component.

    For layers, you can't add a given game object to more than one layer, if you do that through code you will receive a warning about that.
     
  3. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    Yeah, it was always weird to me that Unity limits the tags to one, because I frequently need I more than one. The DOTS has a nice system to use empty entities as a tagging system, because you can easily sort and query based on them, but if you're not using DOTS, then you're out of luck.
     
    Ruchir, FernandoMK and JesOb like this.
  4. FernandoMK

    FernandoMK

    Joined:
    Feb 21, 2017
    Posts:
    178
    Exactly, that would be very good.:D
    in fact a search scheme in the scene by tag, it would be very good too as we do in DOTS, to filter objects in the scene by tags.

    It may be, but it would be MUCH better if it were the tags themselves, working with them is much more practical for various situations, and with the unity visual script, it would certainly be even better:)
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Don't use tags! Especially don't use more than one!

    Wanna know if it's an Archer? Then GetComponent<Archer>(). Want to not have a bunch of archer scripts, but do things in a more data-oriented way? Have a
    Transform[] archers
    .

    If you really want to attach a bunch of arbitrary data to things, but don't want to have a central database of the things, and don't want that data to have types? Make a tag-component that stores all the possible tags as a bit-mask, so you're at least not depending on spelling the tag correctly and comparing by string.


    Or get one of the million free asset store things that are wrappers around things having a List<string> tags attached to them.
     
    Cathair, Ruchir, JoNax97 and 4 others like this.
  6. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    325
    But not Performant.
    I would much rather have a performance boost to both FindObjectWithTag and FindObjectOfType.

    Both of them should be dictionary look ups, not linear iteration like they are currently using.

    But yes, I agree that multiple Tags is a Quality-of-Life feature that is waaayyy overdue.
     
    FernandoMK likes this.
  7. Bezoro

    Bezoro

    Joined:
    Mar 16, 2015
    Posts:
    133
    It would be nice if they made tags more useful yeah, right now I completely disregard them as a feature 99% of the time.
    I think I only use them to mark stuff with the EditorOnly tag rarely.
    With the new changes to Camera.main performance it also made the MainCamera tag not utterly useless to me.
    But if they could improve the system so that you could use multiple tags per object and have a way of checking for them by not using strings that would be pretty cool.

    I've been using ScriptableObjects as tags for a while now, super simple to set up and super flexible.
    It's not the most performant or memory efficient option but as long as you don't need hundreds of tags per object being checked multiple times per frame it's perfectly fine.

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace YourNamespaceHere
    6. {
    7.     [CreateAssetMenu(fileName = "Tag_", menuName = "TagSystem/Tag")]
    8.     public class Tag : ScriptableObject
    9.     {
    10.         public static bool HasTag(Tag tagToCheckFor, List<Tag> tagsToCheckAgainst)
    11.         {
    12.             for (var i = 0; i < tagsToCheckAgainst.Count; i++)
    13.             {
    14.                 if (tagsToCheckAgainst[i] == tagToCheckFor)
    15.                 {
    16.                     return true;
    17.                 }
    18.             }
    19.             return false;
    20.         }
    21.     }
    22. }
    23.  

    Then all you'd need is say a component with a List<Tag>.

    I would agree with Baste though, using tags a lot can actually bite you and I'd prefer to go with actual types.
    One thing that did happen to me once was Unity just lost all references to all tags in all objects at some point, were I not using a VCS I'd have been in serious trouble. That made me pretty weary of them ever since.
     
    Last edited: Nov 3, 2020
    davidrochin, FernandoMK and NotaNaN like this.
  8. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Hey! We have done exactly this :) and it’s in 2021.1! Though I can’t recall exactly which alpha build it arrived in...

    We basically generalised the same improvement we made for Camera.main so every tag has a dedicated list of associated GameObjects - no more searching for the right GameObject(s)!

    EDIT: I realised I misread part of the post I replied to - we haven’t made any improvements to FindObjectOfType (that I’m aware of). Just tags :)
     
    Last edited: Nov 3, 2020
    DrViJ, LeonhardP, FernandoMK and 5 others like this.
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    It's listed in the 2021.1.0a2 release notes "improvements" section:
    https://unity3d.com/unity/alpha/2021.1.0a2
    • Scripting: GameObject.FindGameObjectWithTag and GameObject.FindGameObjectsWithTag are much faster.
     
    kfir124, Prodigga, FernandoMK and 4 others like this.
  10. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    325
    Ha ha ha! Nice!

    I didn't realize you guys had already made the change! (I should've suspected it though). :p

    Now we just need multi-tag functionality and we'll be good-to-go. :D
     
    FernandoMK likes this.
  11. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    And one step closer to DOTS :)
     
    Ruchir and NotaNaN like this.