Search Unity

Question about if this = true && other name is this || this name then do something

Discussion in 'Scripting' started by Unlimited_Energy, Feb 17, 2018.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Hi everyone. I wanted to check and see if what Im writing is interpreted in unity as such.

    Im trying to say:
    if (RunCollisionOnce == true && other.gameObject.name == "ninjato" || other.gameObject.tag == "Weapon")

    I need it to read as, if RunCollisionOnce == true AND IF other.gameObject.name == "ninjato" OR ITS NAME IS other.gameObject.tag == "Weapon"

    I wanted to make sure its not saying if RunCollisionOnce == true && other.gameObject.name == "ninjato" OR IF ONLY other.gameObject.tag == "Weapon") then do something.

    I want to make sure this is not read as if the first two are true together OR the second one only is true then run. I want it to run only if the first statement is true AND the other game objects tag or name is ONE of the two ALSO.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Without parantheses, the expression will be the same as (A && B) || C, because ANDs do usually have higher precedence than ORs.

    Your attempts to describe the desired logic are a bit confusing, but it seems you want A && (B || C).

    Even if you wanted the first logic, it'd be good to put parentheses for the sake of readability. You wouldn't have those moments in which you need to think about precedence.

    If you want to be any clearer, you can use something like:

    Code (csharp):
    1.  
    2.  
    3. var hasNameOrTag = (other.gameObject.name ==  "ninjato") || other.gameObject.CompareTag("Weapon");
    4. // or the longer version
    5. // var hasNinjatoName = other.gameObject.name == "ninjato";
    6. // var hasWeaponTag = other.gameObject.CompareTag("Weapon");
    7. // var hasNameOrTag = hasNinjatoName || hasWeaponTag;
    8.  
    9. if (RunCollisionOnce && hasNameOrTag)
    10. {
    11.     ...
    12. }
    13.  
     
  3. elpirata999

    elpirata999

    Joined:
    Jul 27, 2019
    Posts:
    4
    so confused is || and or is &&?
     
  4. || = or
    && = and
     
    elpirata999 likes this.
  5. elpirata999

    elpirata999

    Joined:
    Jul 27, 2019
    Posts:
    4
    Which is AND and Which is OR? between these two codes "||" "&&"
     
  6. elpirata999

    elpirata999

    Joined:
    Jul 27, 2019
    Posts:
    4
    Thanks.................
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    For the sake of completeness, strictly speaking | = 'Or' and & = 'And', whereas || = 'OrElse' and && = 'AndAlso' (Not sure if the names are universally used, but it's how I originally saw them referred to and how I differentiate them in my mind).

    Take a look at the documentation for short circuited Boolean operators to see how they differ from the others. If the difference isn't understood, incorrect usage can lead to undesired behaviour.
    https://docs.microsoft.com/en-us/do...l-operators#conditional-logical-and-operator-
    https://docs.microsoft.com/en-us/do...l-operators#conditional-logical-and-operator-
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    And to beat a dead horse, hare are some guidelines I try to adhere to, simply because it makes life so much easier to me:
    • NEVER compare a bool to true or false. Always write "if (myBool) { do this }" or "if (!myBool) { do that}
    • Always use parantheses. It may be annoying to write, but you won't fall into the order of precendence trap, especially if you are using a not (!) operator.
    • Never try to invert an AND or OR operator in your head. I've got more than 30 years of programming experience under my belt, and simply know that I'll screw up because I'm human.
    • Use linebreaks for clarity, i.e. when you have multiple guards strung together by a conjunction, use a linebreak
    Code (CSharp):
    1. if ((a < b) &&
    2.     (a < x) &&
    3.     (stillAlive) ) {
    4.        do some funky stuff
    5. }
    And finally: if you are mixing && and || in the same expression without parantheses, you are probably getting it wrong.