Toggle visibility of group of objects

I have scripted the toggling of the visibility of an object with a hotkey:

var Target: Transform;
 
function Update()
 
{
 
    if (Input.GetKeyDown(KeyCode.V))
 
  Target.renderer.enabled = !Target.renderer.enabled;
 
}

How do I modify this to instead target any objects with the tag ‘Hide’?

If children inherited visibility I would use the hierarchy to group this function to objects.

Thanks for the help :slight_smile:

I’m not sure there is another way but to use a FindObjectsOfType function:

You search for renderer and check if it has the tag ‘Hide’.

You can find all the GameObjects with a tag by using GameObject.FindGameObjectsWithTag(string tag). http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

The follow code is in c# but hope it might help you a bit.

        foreach (GameObject gameObj in GameObject.FindGameObjectsWithTag("Hide"))
        {
            if (gameObj.renderer != null)
            {
                gameObj.renderer.enabled = !gameObj.renderer.enabled;
            }
        }