How can I "hide" GUI buttons and boxes?

I’m looking for a way to keep certain GUI buttons and boxes hidden (i.e., renderer disabled) until needed. Using the “renderer.enabled” commands works just fine for objects, but is there any way to do this for GUI’s?

Just don’t draw them in OnGUI(). You could either check a Boolean – for example:

void OnGUI() {
    if (guiEnabled) {
        ...draw GUI controls...
    }
}

Or better yet disable the script that contains the OnGUI method. Even empty OnGUI calls require a significant amount of overhead, so this is the most efficient way to do it.

Would that work for only selected GUI items then? I have some buttons I want constantly displayed, others that should be displayed only when certain objects are rendered (essentially buttons used for closing or modifying certain windows).

If you use one big OnGUI(), then you’ll need to conditionally draw only what you want to display. For example:

void OnGUI() {
    ... draw controls that are always visible ...
    if (showControlA) ...draw control A...
    if (showControlB) ...draw control B...
}

If you haven’t read the GUI Scripting Guide yet, please check it out.

If you put them in separate scripts, you can disable the script(s) containing the OnGUI() code for the controls you want to hide.

You can, of course, get much more sophisticated in how you handle GUI drawing. But I recommend you start small with one of the approaches above and build it up gradually.

ummmmm ok thanks I guess. I can’t find anything in the GUI Scripting Guide on this. I use several smaller scripts, so maybe I’ll try to figure out how to activate/deactivate them selectively. In the meantime, I think stacks of clickable objects will have to suffice. I appreciate the help.

The problem is the GUI button isn’t showing up at all, at any point -regardless of whether or not the parent object is visible. Any chance you can spot what I’m doing wrong here?

#pragma strict

renderer.enabled = false;
collider.enabled = false;
GUI.enabled = false;

function PlanetaryMaps () {
	renderer.enabled = true;
	collider.enabled = true;
	GUI.enabled = true;
}

if (GUI.enabled) {
		if (GUI.Button (Rect (200, 200, 50, 50), "EXIT"));
		renderer.enabled = false;
		collider.enabled = true;
		GUI.enabled = false;
		BroadcastMessage ("CloseMaps");
}

A couple of things, first of all you need to have your GUI code wrapped into the special function OnGUI(). This is a function that is called for every GUI event.

Secondly your if conditional isn’t going to do anything, you’ll want to place the code that runs when the conditional is true within braces.

Your code should probably look something like this.

#pragma strict

// Have these in Start() instead, or Awake().
function Start() {
    renderer.enabled = false;
    collider.enabled = false;
    GUI.enabled = false;
} 

function PlanetaryMaps () {
    renderer.enabled = true;
    collider.enabled = true;
    GUI.enabled = true;
}

function OnGUI() {
    if (GUI.enabled) {
            if (GUI.Button (Rect (200, 200, 50, 50), "EXIT")) {
                renderer.enabled = false;
                collider.enabled = true;
                GUI.enabled = false;
                BroadcastMessage ("CloseMaps");
            }
    }
}

The button is now coming up visible from the beginning. Wow, this is frustrating.

I should pay more attention, that’s cause you are trying to control whether or not the GUI shows up by toggling GUI’s enable property.

So that isn’t really what you want ;). Instead define your own private variable to control that flow. Something like this.

#pragma strict

private var _showGUI = false; 

// Have these in Start() instead, or Awake().
function Start() {
    renderer.enabled = false;
    collider.enabled = false;
} 

function PlanetaryMaps () {
    renderer.enabled = true;
    collider.enabled = true;
    _showGUI = true;
}

function OnGUI() {
    if (_showGUI) {
            if (GUI.Button (Rect (200, 200, 50, 50), "EXIT")) {
                renderer.enabled = false;
                collider.enabled = true;
                _showGUI = false;
                BroadcastMessage ("CloseMaps");
            }
    }
}

Brilliant - it worked perfectly! Thank you - and I assume this will work for GUI boxes, text, etc as well?

Yep, anything within the if (_showGUI) will not appear if _showGUI is false and will appear if it is true.

That’s awesome. It should cut the number of objects I’m using about by half. Thank you.