How to toggle object visibility with keyboard button?

Hi, ive been searching for hours for a solution to this but everyone seems to say something different… and none have worked for me.

All i want to do is press a single button to toggle the visibility of an object. Not sure if it makes a difference, but the objects in question is under an empty gameobject parent (as ideally there would be multiple objects all being turned off via control of the parent).

Ive tried both with the parent and just targeting the object itself but obviously neither works. Ive tried so many different codes and variations that theres no point in posting them all here really. Id just like to know what particular methods work for you when essentially turning on and off an objects visibility. Full code please, im a noob and have no idea how to fill in the blanks :stuck_out_tongue:

Thanks for any help/suggestions - they are desperately needed and greatly appreciated! :smiley:

This should do it

#pragma strict

var isVisible : boolean = true; //Start as on. 

function Start () {

function Update () {
    if (Input.GetKeyDown("space")) {
		if(isVisible == true){
			ToggleVisibility(false); //Tell toggle inside ToggleVisibility to be false
			isVisible = false;
		} else {
			isVisible = true;

function ToggleVisibility(toggle : boolean) { 
    var renderers : Renderer[] = gameObject.GetComponentsInChildren.<Renderer>();
    for (var r : Renderer in renderers) {
		if(toggle == true){
			r.enabled = true;
		} else {
			//Toggle is not true so must be false
			r.enabled = false;