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

Referencing multiple tags in IF statement...

Discussion in 'Scripting' started by HC_Juzzi, Dec 10, 2014.

  1. HC_Juzzi

    HC_Juzzi

    Joined:
    Apr 26, 2013
    Posts:
    3
    Hey people!

    My little project contains 7 different in-game states. I use tags to determine the state. One of the gameObjects reacts to 2 of these states out of the 7. What is the easiest way to do this...below is my current code:

    void OnTriggerEnter(Collider other)
    {
    if (other.tag != "stateOne")
    {


    I tried/Want to achieve:

    void OnTriggerEnter(Collider other)
    {
    if (other.tag != "stateOne" || "stateTwo")
    { ...DEATH ENSUES


    However this does not work, mentioning "Operator || can't be used on type BOOL/STRING"

    Any combinations of operators I can use to make my colliders only react to 2 of the 7 tags?

    Cheers again!
     
  2. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    To react to 2 tags...

    Code (CSharp):
    1. if (other.tag == "stateOne" || other.tag == "stateTwo")
    2. {
    3.   // other.tag is either "stateOne" or "stateTwo"
    4. }
    Jeff
     
    HC_Juzzi likes this.
  3. HC_Juzzi

    HC_Juzzi

    Joined:
    Apr 26, 2013
    Posts:
    3
    This works, thank you...

    However as I only wanted the Collider to react to 2 of the 7 states, with this code it means I must:
    I should have premised in saying this is a "death" script which OnTriggerEnter kills the player if their not in stateOne/Two

    if (other.tag == "stateThree" || other.tag == "stateFour" || other.tag == "stateFive" || other.tag == "stateSix" || other.tag == "stateSeven")

    How would code like this effect performance? Is there a more efficient way to say "If not in these 2 states, proceed to script"?

    Cheers!

    Edit: Spelling.
     
  4. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    Code (CSharp):
    1. if (other.tag != "stateOne" && other.tag != "stateTwo")
    2. {
    3.     // other.tag is NOT "stateOne" or "stateTwo"
    4. }
     
    HC_Juzzi likes this.
  5. HC_Juzzi

    HC_Juzzi

    Joined:
    Apr 26, 2013
    Posts:
    3

    Perfect, Gentleman and a Scholar you are good sir.