x


[Closed] 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
more ▼

asked Jun 28 '11 at 03:26 PM

Dávid Debnár gravatar image

Dávid Debnár
780 42 48 55

(comments are locked)
10|3000 characters needed characters left

The question has been closed Oct 03 '11 at 05:08 PM by Dávid Debnár for the following reason:

The question is answered, right answer was accepted


2 answers: sort voted first

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.

more ▼

answered Jun 28 '11 at 03:45 PM

flaviusxvii gravatar image

flaviusxvii
3k 13 19 43

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 28 '11 at 03:43 PM

Tommy gravatar image

Tommy
156 20 22 27

(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x841
x427
x389
x304
x189

asked: Jun 28 '11 at 03:26 PM

Seen: 2214 times

Last Updated: Apr 21 '12 at 02:46 PM