Enable/disable renderer based on GUI button press

Hi everyone. I’ve been trying to get different objects to render, based on different GUI buttons that have been pressed, but I haven’t been able to get it to work. The final idea is for the player to be able to customise what they look like, by enabling and disabling renderers. If anyone could give some pointers on what I should do to approach this, that would be great. Thanks for any input.

you include your button code somewhere in the OnGUI function, and simply turn your gameobject’s renderer on and off.
In case you have mouse interaction via a collider, you must disable the collider too.

function OnGUI(){
   if (GUI.Button (Rect (100,5,100,30), "press me")) {
      renderer.enabled = false; //turn off renderer
      collider.enabled = false; //turn off mouse interaction
   }
}

now in case you have one button per object, you can include this OnGUI function in your object’s script, changing the button’s position. and in case you want the button to function as a toggle on/off (which i just guess is what you want to do?) you can use the NOT (!) function to invert the visibility on each press:

function OnGUI(){
   if (GUI.Button (Rect (100,5,100,30), "press me")) {
      renderer.enabled != renderer.enabled; //toggle renderer
      collider.enabled != collider.enabled; //toggle mouse interaction
   }
}

and in case you want all your buttons GUI code to be in a single script, not assigned to your gameobjects, you must refer to them through some extra code (it’s up to you to decide your best way)

someObject.renderer.enabled != renderer.enabled;
someObject.collider.enabled != collider.enabled;

and you can get your someObject at Startup of your app/game using a find like this…

private var someObject:GameObject;
function Start(){
   someObject= GameObject.Find("yourObjectName")
}

or you can assign your gameobject to the script and not having to find it just using

var someObject:GameObject;

and dragging-dropping your object from the hierarchy to the “some object” slot created by this script.

I hope this helps!