Turning On & Off Renderers

Hello,

I have a game empty that has a load of game objects in it. I want to turn this empty off so that all the children will not be visible. Although, I don’t want to use SetActive(false), as this will shutdown the scripts too. I only want to turn off the renderers.

I also want the ability to turn them back on too, only issue is that by default, some game objects have their renderers turned off anyway, so I need to make a system that will check if they were already turned off, before being turned off, when being turned on… :confused:

So I firstly need to reference all the renderers that are already turned off to being with by putting them all into an array, before turning off the renderers. Once done, I can turn off all the renderers (even the ones that are already turned off, as it doesnt matter).

Then, then turning them on, I need to check my referenced renderers against the current renderer in the loop, if they match, then leave them off, otherwise, turn them back on.

//turn on and off renderers, but ONLY the ones that were turned on to being with
var alreadyTurnedOff = new Array();
function TableRenderer(on : boolean){

	var renderers = gameContent.GetComponentsInChildren(Renderer);


	for(var r : Renderer in renderers){

		//turn off renderers, make sure we reference the ones that are already turned off - so we dont turn them on when activating
		if(on == false){

			//removed cached renderers
			alreadyTurnedOff.Clear();

			if(r.enabled == false){
				alreadyTurnedOff.Add(r);
			}

			r.enabled = false;
		}

		//only turn on the non reference objects
		if(on == true){

			for(var i = 0; i < alreadyTurnedOff.length; i++){

				if(r == alreadyTurnedOff*){*
  •  			//leave off*
    
  •  		}*
    
  •  		else*
    
  •  			r.enabled = true;*
    
  •  	}*
    
  •  }*
    
  • }*
    }
    Issue is, it doesn’t work! All my renderers still turn on, no matter what?!

There are a couple of small errors in yours. This works for me,

private var alreadyTurnedOff : Array = new Array();
function TableRenderer(on : boolean) {
	var renderers : Renderer[] = gameObject.GetComponentsInChildren.<Renderer>();

	if (on) {
		for ( var r : Renderer in renderers) {
			r.enabled = true;
		}
		for ( var o : Renderer in alreadyTurnedOff) {
			o.enabled = false;
		}
	} else {
		alreadyTurnedOff.Clear();
		for ( var r : Renderer in renderers) {
			if (!r.enabled)
				alreadyTurnedOff.Push(r);

			r.enabled = false;
		}
	}
}