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 :slight_smile:

  • David

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.

Simple solution would be to just attach the script to the childs. Maybe.