Toggling menu screen with key press...

Hey guys! i’m not too sure if this should be here or in the GUI forum but w/e…Im trying to make my GUI window pop open when I hit a button and close when i hit it again, fairly simple, but im having some beef… here’s what I got:

var  statsUp : boolean;
var labelRect : Rect;
function Update () {


	
  if (Input.GetKeyDown("r")) {
        if(statsUp) {
            statsUp = !statsUp;
    
	}
        else {
            statsUp = statsUp;
                   }
    }
	
}

	function OnGUI() { 
		if (statsUp){
			GUI.Label(labelRect, "Inventory Window Goes Here"); 
		}
	}

This will close the window if its open, but not open it if its closed. I’ve tried few diff. variations checking to see if !statsUP Input.GetKeyDown, as well as Input.GetKeyUp, and even switching the order it checks them in, i still can’t figure it out, any suggestions?

You might need to give statsUp a default value:
var statsUp : boolean = false;

Not sure though; in some implementations of ECMAScript, its default value will be null, but in UnityScript, who knows?

Also, did you give your labelRect a value?

Yeah, both values are exposed in the editor have been adjusted, I can see the label just fine, and I can turn it off, just can’t turn it back on. Thanks for the suggestion though!

If it’s supposed to toggle each time you press the button, shouldn’t it be just this?

function Update()
{
	if (Input.GetKeyDown("r")) 
	{
		statsUp = !statsUp;
	}
}
1 Like

Awesome! That definitely did the the trick…Sometimes the simplest solution is best, I guess I just didn’t understand the logic behind it, I don’t see where statsUp is ever set to true here to allow the window to be drawn.

statsUp is false by default. The line essentially says, “set statsUp to the opposite of what statsUp currently is”

So the first time when statsUp is false:

statsUp = ![false]

The ! makes true/false the opposite:

statsUp = true

Then later on when you want to hide it, the line represents:

statsUp = ![true]

which resolves as:

statsUp = false

Ahhh, thanks for clearing that up, i was only seeing it as !statsUp meaning that statsUp was only false, i didn’t know ! toggled boolean values; Great to know in the future. Thanks for fizixin it yo!

Thanks for all the help guys, I got it all up and running and made it so the player could switch among different menus once it was open… heres the code:

//-------------------------------------------------
//
//                Pause Menu 
//
//-------------------------------------------------
var nativeVerticalResolution = 1200.0;
var gskin : GUISkin;
var  statsUp : boolean;
var labelRect : Rect;
var inventoryBG : Texture2D;
var statsBG : Texture2D;
var missionBG : Texture2D;
var mapBG : Texture2D;
var currWindow = 1;

function Update () {


// Toggle Menu Display Variable On or Off
if (Input.GetKeyDown("r")) 
   { 
	currWindow = 1;
	statsUp = !statsUp;  	  
   } 
   
if (Input.GetKeyDown("m")) 
   { 
	currWindow = 4;
	statsUp = !statsUp;  	  
   } 

// Change Menu Display Based On User Input
   		if (statsUp  Input.GetKeyDown("right")  currWindow < 4){
			currWindow++;
		}
		
		if ( statsUp  Input.GetKeyDown("left")  currWindow > 1){
			currWindow--;
		}
		
   
}

// Draw Correct Menu According To Variable
	function OnGUI() { 
	GUI.skin = gskin;
	GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1)); 
		if (statsUp){
			switch (currWindow){
			case 1:
			GUI.Box(labelRect, GUIContent(inventoryBG));
			break;
			case 2:
			GUI.Box(labelRect, GUIContent(statsBG));
			break;
			case 3:
			GUI.Box(labelRect, GUIContent(missionBG));
			break;
			case 4:
			GUI.Box(labelRect, GUIContent(mapBG));
			break;
			}
		}
		

}

Now all I gotta do is make the game pause in the background and I’m golden :smile: