SetActiveRecursively for renderer and collider

Hello,

SetActiveRecursively() turned out for me to be a god given function. I use it everywhere in my javascript now.

But now I have a problem. In one case I dont want to switch off the whole gameObject and it’s children, I want to switch of renderer only and it’s childrens renderer (includes MeshRenderer and SkinnedRenderer) and collider and it’s childrens collider , IF there are any renderer and collider in the childrens components list.

I don’t have any idea how to solve this. A similar function to SetActiveRecursively would be nice …

Any help?

protected void DeactiveRendererAndCollider(GameObject gameObject) {
if (gameObject.renderer) {
gameObject.renderer.enabled = false;
}

	if (gameObject.collider) {
		gameObject.collider.enabled = false;
	}

	foreach (Transform child in gameObject.transform) {
		DeactiveRendererAndCollider(child.gameObject);
	}
}

In the end it turned out to be good to only disable renderer and collider for the parent object and deactivate the child completely. Especially for one reason: The code above detects only one child, but in some cases this child also has a child. So it needs to be done recursively. And this is where my beloved SetActiveRecursively() comes in. :slight_smile:

Here’s the final code snippet for javascript:

		myGameObject.renderer.enabled = false;
		myGameObject.collider.enabled = false
		
		for (var child : Transform in myGameObject.transform) {
		
      		if (child.renderer) {
       			child.gameObject.SetActiveRecursively(false);
       			
    		}

   		}