Search Unity

How can I write "or" in c# script

Discussion in 'Getting Started' started by Pollawat, Jul 12, 2018.

  1. Pollawat

    Pollawat

    Joined:
    Aug 27, 2016
    Posts:
    192
    Hello sir, I'm a newbie and sorry if this is a stupid question.

    Code (CSharp):
    1. if (other.gameObject.tag != ("StealthKillA") ("StealthKillB")("StealthKillC")("StealthKillD")) return;

    This code is error no doubt, as you can see I got 4 tags with a similar idea and I try to write it into c# script. How do I tell in c# that "if tag not equal to StealthKillA or StealthKillB or StealthKillC or StealthKillD" ... return;

    For now, I solve this by writing the hold thing again 4 times (Stupid I know) but how to write it correctly?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    boxhallowed and Pollawat like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You actually probably want 'and' for this one, not or. Boolean logic doesn't quite work the same way as spoken logic.

    Code (CSharp):
    1. if (other.gameObject.tag != ("StealthKillA")
    2.     && other.gameObject.tag != ("StealthKillB")
    3.     && other.gameObject.tag != ("StealthKillC")
    4.     && other.gameObject.tag != ("StealthKillD")) return;
     
    Pollawat likes this.
  4. Pollawat

    Pollawat

    Joined:
    Aug 27, 2016
    Posts:
    192
    Thank you, sir. This helped me a lot.