Hello everyone. i just imported a 3d model with two materials from blender and i want to change one of the materials with code and i tried using MeshRenderer.materials[0] = myMaterial but it doesn’t work
can anyone help me?
Thanks!
1 Answer
1You can’t change an individual material. Instead, copy them all out, update the one you want then write them all back.
Material[] allMaterials = gameObject.GetComponent.<Renderer>().materials;
allMaterials[4] = material6;
gameObject.GetComponent.<Renderer>().materials = allMaterials;
The documentation says: “If you want to change some materials in it, get the value, change an entry and set materials back.”
Thanks :) you are a life saver!
– UNITYx21Thanks Bro!
– UNITYx21Mee too...
– jean-noellevels3d