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

need help!!

Discussion in 'Scripting' started by KviZk089, Feb 12, 2015.

  1. KviZk089

    KviZk089

    Joined:
    Sep 3, 2013
    Posts:
    1
    Hi,
    I'm writing a radar script based on tags and I was thinking if I can write it a little bit different. My code looks like this:
    void OnTriggerEnter (Collider other)
    {
    if (other.gameObject.tag == "Enemy")
    {
    myTarget = other.gameObject.transform;
    audio.Play ();
    }
    else if (other.gameObject.tag == "EnemyAir")
    {
    myTarget = other.gameObject.transform;
    audio.Play ();
    }
    else if(other.gameObject.tag == "EnemyTank")
    {
    myTarget = other.gameObject.transform;
    audio.Play();
    }
    }

    }
    now I don't like so many else if statements so I was wondering can and how can I write it i one if statement ?
     
  2. Megolas

    Megolas

    Joined:
    Apr 6, 2013
    Posts:
    25
    You can use the || (or) keyword

    if (other.gameObject.tag == "Enemy" || other.gameObject.tag == "EnemyAir") {

    }

    You can chain those as much as you want

    if (other.gameObject.tag == "Enemy" || other.gameObject.tag == "EnemyAir" || other.gameObject.tag == "Enemy3") {

    }