Draw / don't draw buttons based on boolean variable

I have a feeling I’m missing something really simple, but I can’t get this to work. I’m just having a menu item (quit) change to “sure?” after a click.

With the debug log in there it shows quitToggle is staying true, regardless of multiple clicks (it should at least change to false after 1 click, right?). But if I move the debug log to the other side of the variable change, it prints ‘false’ every click instead. I’m guessing its something to do with the variable not sticking around by the time the GUI updates next frame? Anyway, I’m stumped. Help appreciated, thanks!

I have my “public bool quitToggle = true;” after monobehavior but before void onGUI. I did try swapping it around in various places, but no luck. Sorry, I’m kinda new :smiley:

// Quit Game Button (ask if you're sure)
		if (quitToggle = true) {
			if (GUI.Button (new Rect (menuPosX+5,menuPosY+110, buttonSizeX, buttonSizeY), "Quit", darkStyle)) {
				Debug.Log(quitToggle);
				quitToggle = false;
			}
		}
			
		// Yes I'm sure button
		if (quitToggle = false) {
			if (GUI.Button (new Rect (menuPosX+5,menuPosY+110, buttonSizeX, buttonSizeY), "Sure?", darkStyle)) {
				System.Diagnostics.Process.GetCurrentProcess().Kill();
			}
		}

Why are you using System.Diagnostics.Process.GetCurrentProcess().Kill(); to kill the game? Application.Quit(); is designed to do it

Application.Quit was crashing. Looked into it and the best answer I found was "its a problem with unity, wait for an update". So I used that as a workaround. Any advice?

Never knew that, always worked when i need it.

1 Answer

1

Read up on the assignment operator = and the equality operator ==. Inside an if statement you almost always need the equality operator.

Thanks much! Learning too many languages at the same time messin' with me.