OR operator not working

I want to make it so the image turns green when either A or D are held but it just stays red, it only turns green if I hold BOTH A and D at the same time

There is zero chance that the OR operator is not working. You logic is wrong. The first if is true if A or D is not pressed so unless you press both it will execute the code inside it.

EDIT: Also @Kurt-Dekker is right, please use code tags and not pictures of the code.

3 Likes

We know the OR operator works.

We also know that photographs of code are not a thing, otherwise one of us would probably have copy/pasted your code and tested it.

But we can’t because we’re certainly not typing your code in!

Use the code formatting button to post all code.

Sounds like you wrote a bug… and that means… time to start debugging!

I’d start by untangling that code, putting temporary results into boolean variables you can debug and print out.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

ALSO, remember this possibility:

Keyboard rollover, jammed keys, bad key combinations:

Test your keyboard here (hat tip to Ryiah!): Online Key Rollover Test - Mechanical Keyboard

1 Like

This isn’t related to 2D or 2D-graphics. Please use appropriate tags, in this case Scripting. I’ll change it for you.

A bit of friendly advice here though is that before posting and making bold statements like this, know that it’s trivial to check your assumptions outside of what you’re doing.

If you were to try:

if (true || false)
{
   Debug.Log("Logical OR is working");
}

… and just change the true/false then you’d know it wasn’t a fundamental piece of the C# language that is broken but instead, some other assumption.

Hope that helps.

Its my first time here and theres like 3 million tags so its pretty confusing, anyways thanks, I didnt know how to title it as I just did || and it didnt do anything, it turned out to be a logical bug and I just turned the first ifs || into && and that worked

Also about the image stuff in other posts yeah I guess thats a better method of showing code

dont know how to reply to everyone so I just replied to the latest one lol

1 Like

NEVER check the same thing again in an if/else if!

Since you only posted a screenshot I’ll make a pseudocode example of how you should do a check like this:

if (a || d)
    panel.color = Color.green;
else
    panel.Color = Color.red

First, always test for the positive first whenever possible. This makes the code easier to reason about. Then use the else keyword if you want the panel to be green if neither a nor d are true, because you already checked for that in the if clause.

1 Like

Ah I see, Im still pretty new to coding so its not the most efficient… This helps out tho, thanks!

Sorry for the late reply btw

I’m suggesting you learn basic coding first to have a grip on how programming language (in facts) works before deep-diving into using Unity. Because Unity as a software, a framework, or a library is also built upon these basic facts. With the basic understanding you can rule out simple logical errors like this by yourself.