- Home /
Set base color of a specific material
Hello, I have an object with script attached to it that changes the base color of the material. However, this model has multiple materials with different names. How do I affect just one of them? Is there a material.Find function I can use? Thank you.
Answer by fherbst · Mar 17, 2012 at 11:19 PM
Access renderer.sharedMaterials instead of renderer.sharedMaterial. Latter just returns the first, former returns all materials as an array, so you can access the one you want by index or go through all and find the one you want to change by name.
Thanks for the response. Okay I tried what you said but it doesn't work and I get an error that says "No renderer in object malenude but a script is trying to access it"
This is my code: public void ChangeColor(float rvalue, float gvalue, float bvalue) { //Color materialcolor = new Color(rvalue,gvalue,bvalue,1); materialcolor.r = rvalue; materialcolor.g = gvalue; materialcolor.b = bvalue; foreach(Renderer part in r) { allmaterials = part.renderer.shared$$anonymous$$aterials; //materiallength = Renderer.shared$$anonymous$$aterials.Length;//allmaterials.Length; materiallength = part.renderer.shared$$anonymous$$aterials.Length; for (int i = 0; i < materiallength; i++) { if (allmaterials[i].ToString() == "malenude-skin") { allmaterials[i].color = materialcolor; } } } }
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Change material color with a three slider 2 Answers
using rgb to change color c# 1 Answer