- Home /
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?
Answer by hvilela · Aug 27, 2012 at 03:35 PM
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);
}
}
Answer by RickTick · Aug 28, 2012 at 03:46 PM
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. :-)
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);
}
}
Look again. The code that I posted is recursive, it disables the children, the children's children and so on. It is recursive, it calls it self.
Oh. Yes, now I see, you're right. But still, my program$$anonymous$$g skills don't allow me to rewrite your code in javascript.