Show/Hide Group of gameobjects with button

Hi, I’ve just spent the last two days trying to learn enough JavaScript to do this but cant figure it out…

I’m trying to create a couple of buttons that will hide/show various groups of objects.

eg. I’ve got a scene with 3 cubes, 3 cylinders, 3 spheres. I want 3 buttons that will toggle visability of each group.

I can get the buttons to show/hide one gameobject using var Cube : GameObject but dont know how to have a group of gameobjects effected. I’ll later want to use this script so I can hide the surfaces, buildings, or utilities of a scene.

I’ve tried arraying a group of objects or using the GameObject.FindGameObjectsWithTag but to be honest I dont really know what I’m doing or if i’m wasting my time??? Any ideas?

Here is my working script: (I guess my question really is how can I set the var CubeGroup as more than one GameObject?)

//ShowHide button script

var CubeGroup = GameObject.FindGameObjectWithTag(“Spheres”);
var Sphere : GameObject;
var Cylinder : GameObject;

function OnGUI () {
//Make a background button box
GUI.Box (Rect (10,10,100,120), “Show/Hide”);

//Hide/Show Group of Cubes
if (GUI.Button (Rect (20,40,80,20), “Cube”)) {
CubeGroup.renderer.enabled = !CubeGroup.renderer.enabled;
}

//Hide/Show 1 Sphere
if (GUI.Button (Rect (20,70,80,20), “Sphere”)) {
Sphere.renderer.enabled = !Sphere.renderer.enabled;
}

//Hide/Show 1 Cylinder
if (GUI.Button (Rect (20,100,80,20), “Cylinder”)) {
Cylinder.renderer.enabled = !Cylinder.renderer.enabled;
}
}

//ShowHide button script

var CubeGroup : GameObject[];
var SphereGroup : GameObject[];
var CylinderGroup : GameObject[];


function OnGUI () {
    //Make a background button box
    GUI.Box (Rect (10,10,100,120), "Show/Hide");

    //Hide/Show Group of Cubes
    if (GUI.Button (Rect (20,40,80,20), "Cube")) {
        for (var cube : GameObject in CubeGroup) {
            cube.renderer.enabled = !cube.renderer.enabled;
        }
    }

    //Hide/Show 1 Sphere
    if (GUI.Button (Rect (20,70,80,20), "Sphere")) {
         for (var sphere : GameObject in SphereGroup) {
            sphere.renderer.enabled = !sphere.renderer.enabled;
        }
    }

    //Hide/Show 1 Cylinder
    if (GUI.Button (Rect (20,100,80,20), "Cylinder")) {
        for (var cylinder : GameObject in CylinderGroup) {
            cylinder.renderer.enabled = !cylinder.renderer.enabled;
        }
    }
}

You can assign the groups via the inspector or FindGameObjectsWithTag.

Wow! Thats awesome!

Can I ask how I use the FindGameObjectsWithTag?

I’m trying to avoid having to add every object, there might be hundreds in the end.

Ok, I figured out how to get the FindGameObjectsWithTag working but I have to manually tag every gameobject in my scene, unlike setting layers where you can select a bunch of objects and assign a layer all at once. damn.

Unless there is a way to select a whole bunch of GameObjects and tag at once, this isnt going to work

I’ll keep trying. Thanks Dman :slight_smile:

I realize this is a bit old, but I wanted to hide all the children of an object and found your thread hoping there would be an easy way to do it. Not finding the answer, I did it myself. I had a modal window with text, icons, borders and shadow which I wanted to toggle on and off. I added the following code to the ModalWindow component.

       public void Show()
	{
		this.gameObject.active = true;
		
		//Show the children
		for(int i=0; i<this.gameObject.transform.childCount; i++)
		{
			this.gameObject.transform.GetChild(i).gameObject.active = true;
		}
	}
	
	public void Hide()
	{
		//Hide the children
		for(int i=0; i<this.gameObject.transform.childCount; i++)
		{
			this.gameObject.transform.GetChild(i).gameObject.active = false;			
		}
		
		this.gameObject.active = false;
	}

Hopefully this helps someone else.

Hi Sam,
This should be simple shouldnt it! I’m trying to also hide groups of objects, but the code kindly posted by you guys seems to work on individual objects when they are added to the CubeGroup in the inspector, but they are not working on groups of items.

Where might i be going wrong? :face_with_spiral_eyes: Do i have to add tags or specific names to the objects? I have attached the script to an Empty object and it works fine at hiding individual meshes.

I sometimes get an error saying that the group might not have a Mesh renderer applied - so i added this component and still no luck at toggling it on and off.

How did you get the GameObjectWithTags to work? Dont know if you know but in Unity 3.5 you can change multiple object settings now in the Hierarchy so you would be able to tag lots of items in one go instead of doing it individually! :razz:

Any help would be awesome…thanks guys.