- Home /
Do something for each material in each renderer in children...
I have a car mesh with separate parts but with same material... I'm trying to change color of all materials called "carMaterial" in all renderers in the child objects of the car object...
I have tried different approaches but with no luck...
CarBodyRenderers= GetComponentsInChildren<Renderer>();
foreach(Renderer rend in CarBodyRenderers){
foreach(Material matt in rend.materials){
if(matt.name == "carMaterial"){
etColor("_Color", carPaint);
}
}
}
Answer by Sondre-S · Feb 13, 2015 at 09:57 PM
Found my answer here: http://answers.unity3d.com/questions/205372/syntax-for-a-material.html
The correct syntax for this is:
if(matt.name == "carMaterial (Instance)")
You're right, sorry. Components share the name with its GameObject, not Objects... stupid me
Answer by hexagonius · Feb 13, 2015 at 09:21 PM
Grab the sharedMaterial instead of all materials of at least one object using it and modify the color. It should automatically change all of them.
Also comparing a materials name with a string actually checks against the GameObjects name (Material, Object name definition)
that won't work, would'nt it change the color of all cars using the same material?
So, (matt.name == "car$$anonymous$$aterial")
is not the right syntax? Then what is the correct syntax?
$$anonymous$$aybe you should create one material per car if it's not a model that's using one in total. That way you won't get your draw calls up. Doesn't matter with Sprite Renderers, there you would be right.
Just don't compare the names since matt.name refers to the car's name, not the material name
Works now, I had to add (Instance) after the name. Btw I have a material for each car, but there may be more than one car of the same type, but with different colors. (For example a red Honda and a white Honda)
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
how do I change the properties of a material that are on the children of an object on script? 1 Answer
Changing specific materials onTriggerEnter 3 Answers
Are Mesh Filters Really Necessary? 1 Answer
Trying to make a side scroller. I'm following a tutorial and getting this error. 1 Answer