- Home /
Make code effect children of a game object
I'm looking to set several objects to invisible but I need to do it through the parent to which the meshes are children. Is there any way I could use the renderer.enabled=false; to effect all of the children from the parent?
Answer by iggy · May 31, 2011 at 07:25 AM
var meshChildren : MeshRenderer[];
meshChildren = gameObject.GetComponentsInChildren.<MeshRenderer>();
for (var mesh : MeshRenderer in meshChildren) {
if(mesh.gameObject!=gameObject)
mesh.enabled = false;
}
source: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Edit: i added a fix thanks to Christian. I tested the code and it works. Hope it helps.
That will work, but beware that GetComponentsInChildren is horribly, horribly misnamed, since it also returns components from the parent object, and not just its children, which one might've otherwise been led to believe. ;)
So unless you mean to disable the parent's renderer as well, you need to check the returned renderers to make sure you're only disabling children.
Does this block of code get called all at once? I shoved it in the update function of a gameobject that is calling it and everything still appeared. I apologize if there's something obvious I'm not understanding.
Edit: I also made a Debug.Log call within the same if statement for the mesh.enabled = false to take effect, but it doesn't seem to show up.
you must put this code in some function first and call that function when you want to hide it.
this is my "HideChildren.js". i add it to parent and when i press "spacebar" it calles the function and hides all children.
function Update () { if (Input.Get$$anonymous$$eyDown ("space")) hideChildren(); }
function hideChildren() { var meshChildren : $$anonymous$$eshRenderer[]; meshChildren = gameObject.GetComponentsInChildren.<$$anonymous$$eshRenderer>(); for (var mesh : $$anonymous$$eshRenderer in meshChildren) { if(mesh.gameObject!=gameObject) mesh.enabled = false; } }
Answer by KennSan · Jun 01, 2011 at 04:48 PM
I can't work out why but using the code that time Iggy spat out a load of errors out at me. However upon digging a lot deeper I found something that is very like your code, but there are some minor code differences:
function Update() {
if (Input.GetKeyDown(KeyCode.Z)) {
DoRender();
} }
function DoRender(){ var children : Renderer[]; children = GetComponentsInChildren.(); Debug.Log (children); for (var i : Renderer in children) { i.enabled = false; } }
However I do thank you for all your patience and help =) If you didn't help I wouldn't have had a clue when I was even warm to being correct!
Your answer
Follow this Question
Related Questions
Game Objects appear in Scene but not in Game view 2 Answers
Can one specify the parent of a gameobject in an array? 2 Answers
Moving a GameObject reffering to the coordinates of the parent Object 1 Answer
Is there a way to make a selected object a child of another object during the game? 1 Answer
How can I attach a gameobject to another without changing its scale. 1 Answer