Search Unity

Unity 5.1.2: C# Conditional OR Not Working

Discussion in 'Scripting' started by ExbowFTW, Jul 29, 2015.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Is there a bug for "||" and "|" in C#? Because when I use it it treats it like an and "&&".

    Code (csharp):
    1.  
    2. if (Input.GetKeyDown ("w") | (Input.GetKeyDown ("s"))) {
    3. anim.SetInteger ("Moving", 1);
    4.  }
    5.  
    When I use this code, I have to press both the "w" key and the "s" key to make the "Moving" integer 1...

    This used to work all the time back in 5.0.1. Has there been any changes? Is this a bug? Did I make a mistake?

    Thanks,
    Exbow
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Conditional OR is expressed with two lines "||". Logical OR is expressed with one line "|" and I've only actually seen it used with bitwise calculations like FLAG enums. When you're using a normal conditional statement, you need to use the former I think.
     
    Ryiah and Kiwasi like this.
  3. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    I tried both | and ||, and they both act like ands.
     
  4. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    I had some trouble with
    Code (CSharp):
    1. if (Input.GetKeyDown ("w"))
    not working last night. Instead I opted for
    Code (CSharp):
    1. if(Input.GetKey (KeyCode.W))
    - Maybe try that?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    There is not a bug with the simplest usage of the most basic logical operators in one of the largest languages in the world.

    The problem is probably in using GetKeyDown, as it uses the values set up in the input manager. I'd look there and check that the key "w" is bound to "w", and the key "s" is bound to "s". Or just use the KeyCode variant.

    Or you've got some other messups in your logic layout somewhere else in the code.
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1. void Update () {
    2.         if ( Input.GetKeyDown("w") || (Input.GetKeyDown("s"))) {
    3.             Debug.Log("DONE");
    4.         }      
    5. }
    Just works fine like it should as OR.

    But instead of "w" i would use

    Code (CSharp):
    1. void Update () {
    2.         if (  Input.GetKeyDown(KeyCode.W) ||  Input.GetKeyDown(KeyCode.S)  ) {
    3.             Debug.Log("DONE");
    4.         }      
    5. }
     
  7. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Okay thanks... By the way I figured it out - it was because another IF-THEN was making "Moving" 0.

    However: Why isn't this working? It won't make Shifting true :(

    Code (csharp):
    1.  
    2. if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
    3. anim.SetBool ("Shifting", true);
    4.  }
    5.  
     
  8. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    That could be a dozen things that have nothing to do with the input- can you put in a debug and make sure it isn't getting there?
     
  9. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    I figured it out :) It was a really stupid mistake... THANKS GUYS
     
  10. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Would be nice if you told us what the error was in the end. But my guess would be in a typo in the animation controller?
     
  11. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    lol yeah that was it. ;)