Unity 5.1.2: C# Conditional OR Not Working

Is there a bug for “||” and “|” in C#? Because when I use it it treats it like an and “&&”.

if (Input.GetKeyDown ("w") | (Input.GetKeyDown ("s"))) {
anim.SetInteger ("Moving", 1);
 }

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

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.

2 Likes

I tried both | and ||, and they both act like ands.

I had some trouble with if (Input.GetKeyDown ("w")) not working last night. Instead I opted for if(Input.GetKey (KeyCode.W)) - Maybe try that?

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.

void Update () {
        if ( Input.GetKeyDown("w") || (Input.GetKeyDown("s"))) {
            Debug.Log("DONE");
        }       
}

Just works fine like it should as OR.

But instead of “w” i would use

void Update () {
        if (  Input.GetKeyDown(KeyCode.W) ||  Input.GetKeyDown(KeyCode.S)  ) {
            Debug.Log("DONE");
        }       
}

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 :frowning:

if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
anim.SetBool ("Shifting", true);
 }

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?

I figured it out :slight_smile: It was a really stupid mistake… THANKS GUYS

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?

lol yeah that was it. :wink: