- Home /
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... :/
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[i]){
//leave off
}
else
r.enabled = true;
}
}
}
}
Issue is, it doesn't work! All my renderers still turn on, no matter what?!
How about adding a script to the children? Like they check wether the Parents Empty is enabled or not?
For example:
if(parent.renderer.enabled == true){
this.renderer.enabled = true;
}else{
this.renderer.enabled = false;
}
And then define in the start() if this child is influenced, or just dont add the script. I don't know if this matches your problem ...
Here's an idea, you have a Empty GameObject with ally our scripts, you then add another empty as a child to the first, then have all the other object as a child to that one, then simple "SetActive(false)" to the second empty? Sounds like it would work!
Answer by craigaw · Jan 17, 2014 at 05:21 AM
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;
}
}
}