- Home /
The question is answered, right answer was accepted
Change material of all the children
Hey guys, I know, that this is probably pretty easy, but I wanna code it fast and my brain is kinda slow tonight. So, I have an object, that has around 10 children and a few of them have other children and a few of the other children of the children have more children. Simply, it goes like 4 levels downwards. I made this
var mat : Material;
function Start () { for (var child : Transform in transform) { child.renderer.material = mat; } }
but it only changes the material of the first level children. I hope that you got what I want :)
David
Answer by flaviusxvii · Jun 28, 2011 at 03:45 PM
This is sort of pseudo-codish.. but it's the right idea.. You need to recurse.
function setMaterial(go : Transform, mat : Material)
{
for(var child : Transform in go) {
child.renderer.material = mat;
setMaterial(child, mat); // This will repeat the process on the child.
}
}
Then call setMaterial on your top level transform with the material you want.
I think this code would be better like this:
function set$$anonymous$$aterial(go : Transform, mat : $$anonymous$$aterial)
{
for(var child : Transform in go) {
set$$anonymous$$aterial(child, mat); // This will repeat the process on the child.
child.renderer.material = mat;
}
}
Your way, the material is set twice, in the main method and in the branch. This way, materials are only set once after the code has recursed to the deepest level.
Answer by Tommy · Jun 28, 2011 at 03:43 PM
Simple solution would be to just attach the script to the childs. Maybe.
Follow this Question
Related Questions
Changing texture of childrens 2 Answers
FindChild/Change color 1 Answer
Change the color of a child 1 Answer