I have some checks to see if a screen is active. The code looks like this:
if (GUI.Button(new Rect(Screen.width / 2 - 10, 50, 50, 30), "Rules")) //Creates a button { if (ruleScreenActive == true) //check if the screen is already active ruleScreenActive = false; //handle according to that else ruleScreenActive = true; }
Is there any way to - whenever I click the button - invert the value of ruleScreenActive
?
(This is C# in Unity3D)
The logical NOT ( ! ) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with boolean (logical) values. When used with non-Boolean values, it returns false if its single operand can be converted to true ; otherwise, returns true .
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
You can get rid of your if/else statements by negating the bool's value:
ruleScreenActive = !ruleScreenActive;
I think it is better to write:
ruleScreenActive ^= true;
that way you avoid writing the variable name twice ... which can lead to errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With