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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Multiple tag using string

Discussion in 'Scripting' started by tawdry, Mar 12, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi guys
    Need to have multiple ways to tag my npc's found this code that states you can use this
    tag name might be enemysniperblue
    tag.Contains(string sniper)

    Code (CSharp):
    1. void OnTriggerStay(Collider other) {
    2.         if (other.tag.Contains(string night )){Debug.Log("hello");}
    3.         int enemyCount = GameObject.FindGameObjectsWithTag("night").Length;
    4.         GameObject[] enemies = GameObject.FindGameObjectsWithTag("night");
    return error Unexpected symbol `night', expecting `.' tried putting "night" but same thing .Also if this does work is ther a way to use it with FindGameObjectsWithTag
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    String.Contains() is a method that expects a string literal;

    Code (CSharp):
    1. if ( other.tag.Contains("night") ) {
    2.     Debug.Log( ".." )
    3. }
    Also, as most "Find" methods are - or can be - quite expensive, you should try to not use them too often;

    Code (CSharp):
    1. GameObject[] enemies = GameObject.FindGameObjectsWithTag( "night" );
    2. int enemyCount = enemies.Length;
     
    DrMobil likes this.
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hey foreau unfortunately it does not work its returns "night" tag is undefined. night is part of the tag enemyknight.
     
  4. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Oh, so you want to find all GameObjects that has a tag that contains "night"? In that case you need to find _all_ the GameObjects of a certain type and do a match on each of their tag.
     
  5. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
  6. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    2 ways:
    1. Get all the objects in the scene or by type and do a foreach cheching for containning string (tag) that you want.
    2. Create a script to store a array of string for you and call it Tags. You might want to add this to the other scripts:
      Code (CSharp):
      1. [RequiredComponent(typeof(yourClassRefTags))]
    Hope this helps.
     
  7. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Was hoping to use Unities tag system instead of a 3rd party guess i'll try out Tag frezy known about it a while so shall see u would think Unity would have got round by now to allowing multi tags.
     
  8. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    If you need to have multiple tags for a single object, I would use empty game objects and parent them to the main object, then search through them and find objects that are tagged as night when the trigger event happens.


    Something like this.

    Code (CSharp):
    1.    
    2.  
    3. void OnTriggerStay(Collider other) {
    4.  
    5. Transform [] allTagObjects = other.GetComponentsInChildren<Transform>();
    6.  
    7. foreach( Transform tagObject in allTagObjects){
    8.  
    9. if(tagObject.tag == "night"){
    10.  
    11. Debug.Log("hello");
    12. break;
    13. }
    14.          
    15.  
    16. }
    17.  
    18.  
     
  9. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    I made empty GO added collider as trigger.This works so I guess it depends on what uses most processing power. I made a post about a asset called tag frenzy Ill see if i can get it to work then stress test the 3 methods..My other option is to create varies tags and have a ton of OR statements then stitch them together.
    Thx for the code
     
  10. aidesigner

    aidesigner

    Joined:
    May 2, 2012
    Posts:
    121
    I share your view and it is possible (Instructions below), which is why I created a multiple tags product using only the Unity tag system. Here are some solutions for anyone facing this issue:
    • A custom tag solution definitely works and allows generating new tags at runtime. However a custom system usually involves a TagManager gameObject and the attaching scripts to any tagged object. This can be cumbersome, especially since Unity has a tag system.
    • In some situation using polymorphism and interfaces can be acceptable. In other words you can make a Flying base class or a IRed interface. Then use GameObject.FindObjectsOfType() to find the desired objects. The Unity documents indicates this API is slow any should not be used in an update loop.
    • Create your own Multiple Tags solution that uses the Unity tag system (Like I did). Assign multiple tags to a gameObject by stitching together multiple tags into a single string. If you delineate the tags with a backslash the Unity tag system will present the tags in a very elegant hierarchical menu. The only thing you may want to create is some custom find tag APIs that will find gameObjects by comparing against tags after splitting.