Using gui buttons to render.enable/render.disable

Good morning, all. Happy Friday.

I am trying to combine to aspects of a scene I have into a more streamlined version. Right now I have a function that will enable/disable a mesh in my scene on a GetKeyDown input. I also have a simple gui that will send me to another scene. Each scene has the gui function so you can navigate between any scene in the project.

So what I have been trying to do ( and failing at so far this morning ) is to set up a gui with a series of buttons, that when pressed, will toggle a mesh on and of.
The closest I can figure it, is I’d have to set a var:GameObject so I can choose which game object I want this to happen to. Then something like if (Gui.Button (Rect (10,10,50,50), “Render Mesh”)) {

  • then i don’t know what would go here- ;
    }

So as you can see. I’m at a total loss on this. Just trying to get this done by the end of the day so I can go visit the grandparents ( 5 hour drive ) :wink: .

Here’s what I have to work with.

function OnGUI () {
	
	GUI.Box (Rect (10,10,100,90), "Turn Stuff On and Off");

	
	if (GUI.Button (Rect (20,40,80,20), "Object 1")) {
		This is where the enabling/disabling would have to go;
	}

	
	if (GUI.Button (Rect (20,70,80,20), "Object 2")) {
		This is where the enabling/disabling would have to go;
	}
}

And here is what I have been using to turn it on and off

function Update () {
    if (Input.GetKeyDown ("2"))
      renderer.enabled = !renderer.enabled;
    if (Input.GetKeyDown ("2"))
       renderer.disabled = !renderer.disabled;
 }

So in short ( too late ) I am wanting to do away with pressing a button to get the mesh on and off and make that a function of a button in my GUI. And I want to be able to set the objects as variables so I can utilize this script for many different objects, thus streamlining the project and making it much more user friendly.

So any help at all would be greatly appreciated. Anything. Can’t keep grandparents waiting!

Thanks a ton in advance, all.
-Richard

Is your GUI script on a different GameObject? If so, you’ll have to Find() it and then access the renderer from there.

http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html

public GameObject YourGameObject;
public GameObject YourGameObject2;

function OnGUI () {
	
	GUI.Box (Rect (10,10,100,90), "Turn Stuff On and Off");

	
	if (GUI.Button (Rect (20,40,80,20), "Object 1")) {
		YourGameObject.renderer.enabled = !YourGameObject.renderer.enabled;
	}

	
	if (GUI.Button (Rect (20,70,80,20), "Object 2")) {
		YourGameObject2.renderer.enabled = !YourGameObject2.renderer.enabled;
	}
}

and link your gameobjects in the inspector, or you can use an array of GameObjects

not sure where you got stuck… I might didn’t understood your question, it’s late friday :smile:

edit: Or you can do

public GameObject YourGameObject;
public GameObject YourGameObject2;

function OnGUI () {
	
	GUI.Box (Rect (10,10,100,90), "Turn Stuff On and Off");

	
	if (GUI.Button (Rect (20,40,80,20), "Object 1")) {
		YourGameObject= GameObject1;
		OnOff();
	}

	
	if (GUI.Button (Rect (20,70,80,20), "Object 2")) {
		YourGameObject = GameObject2;
		OnOff();
	}
}


function OnOff()
{
 YourGameObject.renderer.enabled = !YourGameObject.renderer.enabled;
     }

if it’s in differents script I would use a static YourGameObject

You’re almost there!

var object1 : Renderer;  //assign this in inspector - note that it's of type renderer, so all you have to do is drag in a renderer
var object2 : Renderer;

function OnGUI () {
    GUI.Box (Rect (10,10,100,90), "Turn Stuff On and Off");

    if (GUI.Button (Rect (20,40,80,20), "Object 1")) {
        object1.enabled = !object1.enabled;
    }  

    if (GUI.Button (Rect (20,70,80,20), "Object 2")) {
        object2.enabled = !object2.enabled;
    }
}

And should be pretty straight shooting from there. :wink:

Regards,

-Lincoln Green

ok. If I were to want the function to utilize GameObject.FindWithTag() and set the object that has the renderer being enabled/disabled to “Sphere1”, for example, how would I call the object into the function.
Basically I want the button to enable/disable a zillion objects with the same tag.
Any leads?

Typed in a web browser from an iPod - doubly risky. :wink:

var theTag = "tag you want to enable";

function Toggle(){
  for(var curObj in GameObject.FindGameObjectsWithTag(theTag)){
    curObj.renderer.enabled = !curObj.renderer.enabled; //cycle through all of the gameobjects tagged with your tag and toggle the renderer on or off
  }
}

Regards,

Awesome.

So I got it to work with 1 button, then 2, but when I added the third I got some errors.
Not really sure why.
Also… running on an hour of sleep and close to 3 pitchers of coffee.

If you’re using it with multiple tags, a more general purpose function would be in order:

function ToggleRenderersWithTag(theTag : String){
* for(var curObj in GameObject.FindGameObjectsWithTag(theTag)){
* * curObj.renderer.enabled = !curObj.renderer.enabled; //cycle through all of the gameobjects tagged with your tag and toggle the renderer on or off
* }
}

Just pop that into your script, and then to use it:

ToggleRenderersWithTag("Gas Cap"); //etc