Need help with GUI Scripts.

Basically I’m trying to make it so when I click a button a box will open and then if I click the button again I want to the box to close. Like the characters inventory button in most games.

var customSkin: UnityEngine.GUISkin;

function OnGUI () {
	
	GUI.skin = customSkin;
	
    GUI.Button (Rect (498,543, 40, 40),"");
    
        }

This is the code that I have that will create the button and this is attached to the main camera. When this button is clicked I want the box to appear and then disappear when clicked again. Below is the code for the box:

var customSkin:GUISkin;

function OnGUI () {

GUI.skin = customSkin;
GUI.Box (Rect (515,135, 220, 250),"");

}

I have tried to do it like this below, which showed no errors on the compiler however it did not work.

var customSkin: UnityEngine.GUISkin;
var Guiscript : InventoryBackdrop;

function OnGUI () {
	
	GUI.skin = customSkin;
	
    GUI.Button (Rect (498,543, 40, 40),"");
    
    if (GUI.Button (Rect (498, 543, 40, 40),"")); 
        
        if (Guiscript.enabled == true){
        Guiscript.enabled = false;
        }
        else {
        Guiscript.enabled = true;
        }
     }

The script “InventoryBackdrop” is the box that I showed earlier.

Any help would be greatly appreciated,:slight_smile:
Thanks in advance.

The problem that pops out to me is that you’re stacking buttons. You don’t need the first ‘GUI.Button’ line - the button will be created in the ‘if(GUI.Button)’ bit.

Hope this helps.

Thanks for that, didn’t even realise that I was stacking the two buttons. Still haven’t got the box to show up when I click the button, however getting rid of that button that was stacking is surely a step closer in the right direction I guess. :smile:

Oh, I see the problem now. in your ‘if’ statement, you have a semicolon after your closing parentheses - drop the semicolon and put an opening brace { there.

Minor nitpickiness on my part - a much cleaner way of performing the toggle is:

 if (GUI.Button (Rect (498, 543, 40, 40),"")){
     Guiscript.enabled = !Guiscript.enabled;
}

This will just set the Guiscript to not have the enabled state it used to have.

Hope this helps.

Thanks mate, got it working exactly as I would have hoped now. Probably would have never got it to work without your help. Thanks for the speedy replies too :smile:

Greatly appreciate your help.