- Home /
Why is Unity registering my false boolean as true?
Right now I am designing an instructions scene, and I cannot determine why Unity is registering my false boolean as true. Most of this scripting is just for context and you can totally ignore my first GUI.Label text. I have a boolean "giveTenPoints" which simply loads a GUI.Label that says "+10". But the catch is I only want to have it appear after the user has pressed my GUI.Button. To do this, I have a "giveTenPoints" boolean that becomes true after the user hits the button. But for some reason it is true from the start of the scene and ten points shows up immediately, not waiting for the user to hit the button. My other boolean "hasBeenPressed1" works perfectly fine and only becomes true after pressing the button so that it effectively deletes it. Thank you for the help.
#pragma strict
var myStyle : GUIStyle;
var scrollPosition : Vector2 = Vector2.zero;
private var howFarRight : int = Screen.width;
private var howFarDown : int = Screen.height;
private var difference : int = (Screen.width - 153);
private var centering : int;
var practiceOrangeDot : Texture;
private var orangeCenter : int;
private var hasBeenPressed1 : boolean = false;
private var giveTenPoints : boolean = false;
function Start () {
centering = difference;
centering /= 2;
orangeCenter = centering;
orangeCenter += 40;
}
function OnGUI ()
{
scrollPosition = GUI.BeginScrollView (Rect (0,0,howFarRight,howFarDown), scrollPosition, Rect (0, 0, 0, 1250));
GUI.Label (Rect (centering, 0, 130, 1300), "Dots or Die is a simple game with a simple premise: Tap all the orange dots, and leave the other dots alone. If you do this, you will be handsomely rewarded. Just for touching an orange dot, you will receive ten points. A whole ten points! Here, let’s practice, shall we?\n\n\n\n\n\n\nBrilliant! (assuming you hit the dot, otherwise I’m congratulating you for being disobedient and doing absolutely nothing). Now, hit this red dot.\n\n\n\n\n\n\nSo if you hit a red dot (or any dot that isn’t orange for that matter), you are now out of twenty hard-earned points. I know you’re thinking, that’s ridiculous, if I do my job, I only garner ten points, but then I make a mistake and twenty are deducted. Well, in short, yes… but think about this. Why are there five weekdays and only two days in the weekend? That’s an even worse ratio. Maybe this ostensibly simple game is actually a beautiful allegory acutely highlighting life’s injustices and iniquities? I’m not claiming it is, but there’s certainly room for discussion on it (and room for it on the description in the app store). That’s enough for now though, so I’ll let you play the game and decide. ", myStyle);
if (!hasBeenPressed1) {
if (GUI.Button(Rect(orangeCenter,275,50,50),practiceOrangeDot,myStyle))
hasBeenPressed1 = true;
giveTenPoints = true;
}
if (giveTenPoints == true) {
GUI.Label (Rect (orangeCenter, 285, 50, 50), "+10", myStyle);
}
GUI.EndScrollView ();
}
Answer by robertbu · Jul 23, 2014 at 11:03 PM
You have no brackets ({}) after your 'if(GUI.Button())' line. That means the 'if()' statement will only be applied to the next line. If you want multiple lines inside an 'if()' statement, you need brackets like you use for 'if (!hasBeenPressed1)'.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154826im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI button is being triggered by 2 fingers on screen. 1 Answer
Static var triggerGo? 2 Answers
How to use if statement properly 1 Answer
creating a label from a button click 1 Answer
Placing GUI on the right order 0 Answers