Home > Back-end >  Unity button works even "if" is false
Unity button works even "if" is false

Time:06-12

I was trying to do a shopping system but my buttons worked even if statement were false. I put 10 < 5 on purpose but if statement still executing the code. Method for button

Console output

CodePudding user response:

Ok, so as @kar mentioned please feel free to post actual code samples so others can help. Secondly, when dealing with logical expressions ( ||, &&, etc.) please note that these have an order of operation/precedence like you would see in Mathematics (BODMAS).

e.g. The below code will always execute what's inside the IF block because the state is not 0. If the state was 0, then it would not execute.

if( state!=0 || state!= 1 && 10 < 5)
{
    Console.WriteLine("Logic needs to be fixed.");
}

To be clearer it's also recommended to group logical operations instead of relying on implicit ordering e.g. by changing if( state!=0 || state!= 1 && 10 < 5) to if( state!=0 || (state!= 1 && 10 < 5))

CodePudding user response:

I rearranged the statement like this and get state parameter direclt on object. It did the trick. thank you again Onboardmass correct statement

  • Related