How do I change the color of a material in mesh render?
I am trying to programmatically change the color of the material on a mesh. The mesh has a mesh renderer with 2 materials. While I am only trying to change one, I have not had any luck with getting the one I don't want to change changed either.
Here is my latest attempt at getting this to work:
this.gameObject.renderer.materials[1].color = Color.blue;
I have also tried this.gameObject.renderer.material.color = Color.blue;
which should change the color of all the materials in the mesh renderer but that doesn't work. I've tried typing it like this.gameObject.GetComponent<MeshRenderer>().materials[1].color = Color.blue;
and have just not had any luck getting my mesh to change colors. I've even gone as far as to manually defining each material, assigning them colors and manually adding them to an array then setting the array as the material for the mesh renderer but it still has no effect at all.
Material[] materials = new Material[1];
materials[0] = regularMaterial;
materials[1] = canopyMaterial;
materials[1].color = Color.red;
this.gameObject.renderer.materials = materials;
I have extensively searched nearly every thread on the internet and nothing is working. Can anyone help me with this?
Never$$anonymous$$d I fixed it. After a day of wondering why nothing was working, I fixed it by right-clicking on the component in the editor, and pressing "$$anonymous$$ove up" until it's above the $$anonymous$$esh Filter component. Weird.