Hi All,
Im new at Unity and also not a programmer :roll:
I’d like to add buttons in my application that make things dissaperar when clicked and appear again when clicked again.
For example: to choose to make the roof, or furniture to appear or not at the scene.
How do I do that? Is there a script or documentation? Where can I find it?
Thanks !!!
Easy one. Have your GUI button toggle the following boolean on the gameobject you’re interested in.
renderer.enabled = false;
Here’s a link to the relevant documentation.
I’m trying to do the same thing. Using a GUI button or a simple toggle to switch the visibility of an object on and off.
I’m able to do so using the following script, as many of the posts have mentioned:
function OnGUI () {
// Make the first button. If it is pressed, the biceps femoris will be hidden.
if (GUI.Button (Rect (20,40,150,20), “Biceps Femoris Muscle”)) {
renderer.enabled = false;
}
But, this script above only allows me to turn the object’s visibility off with the GUI button. How do I attach the following toggle script to it?
var toggleBool = false;
function OnGUI () {
toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, “Biceps Femoris”))
renderer.enabled = false;
}
I guess I don’t get how to combine the two scripts.
Thanks,
Meena
I’m not sure if this code will work as I can’t test it (don’t have Unity yet) but it might. It won’t handle any visual toggling of the GUI button, but it should toggle the visibility of the object.
function OnGUI () {
if (GUI.Button (Rect (20,40,150,20), "Biceps Femoris Muscle")) {
if(renderer.enabled == true)
renderer.enabled = false;
else
return;
}
With my luck this won’t do a thing, but it’s worth a shot.
Or you could try this to toggle the renderer:
function OnGUI () {
if (GUI.Button (Rect (20,40,150,20), "Biceps Femoris Muscle")) {
renderer.enabled = !renderer.enabled
}
Forgot about good old exclamation marks. That beats my script.

Rock on - it works!
Thanks so much to both of you - I used the exclamation point one cuz it was shorter, haven’t checked to see if the other script you (Sakar) wrote works…regardless, they’re both useful for me to see. thanks again guys.
var toggleBool = false;
function OnGUI () {
toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Biceps Femoris"))
renderer.enabled = false;
}
Calling a GUI.Toggle always returns a true/false value.
So you can say renderer.enabled = toggleBool;
That way whether the renderer is enabled is always concurrent with the toggle function. Preventing any slip-ups along the way.
The script that you listed:
var toggleBool = false;
function OnGUI () {
toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Biceps Femoris"))
renderer.enabled = false;
}
is the same as my initial script that I posted, however, which didn’t work…:
var toggleBool = false;
function OnGUI () {
toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Biceps Femoris"))
renderer.enabled = false;
}
I don’t know why it didn’t work though - do you? Should the last line of the script
be renderer.enabled = toggleBool;
instead of renderer.enabled = false;
?
Is that what you were saying?..and thank you Coach Carter.
The easiest way to do this is probably
function OnGUI () {
renderer.enabled = GUI.Toggle (Rect (25, 25, 100, 30), renderer.enabled, "Biceps Femoris");
}
That will make a toggle button. If you want to make it look like a normal button, pass in a GUIStyle to it, like this:
function OnGUI () {
renderer.enabled = GUI.Toggle (Rect (25, 25, 100, 30), renderer.enabled, "Biceps Femoris", "Button");
}
Yeah, sorry. That would be what the final code would look like.
var toggleBool = false;
function OnGUI () {
toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Biceps Femoris"))
renderer.enabled = toggleBool;
}
It didn’t work because you were always setting the value of renderer.enabled to false regardless of what value the toggle was giving.
Now it should always set the renderer.enabled to whichever value the GUI.Toggle is currently on.
makes sense.
so I don’t have to declare
var toggleBool = true
and do a separate script for this scenario, right?
B/c this would be the default alternative for the renderer.enabled = toggleBool, since there’s only true or false?
Using the code provided by nicholas:
function OnGUI () {
renderer.enabled = GUI.Toggle (Rect (25, 25, 100, 30), renderer.enabled, "Biceps Femoris", "Button");
}
You wouldn’t have to declare toggleBool.