I am procedurally generating a cube mesh in the editor. Each of its faces can be enabled or disabled. Each enabled face is created as a separate submesh and materials are assigned to MeshRenderer.sharedMaterials accordingly. However, in MeshRenderer’s inspector, materials does not reflect that assignment, whatever I manually assign does not change. This is the code I use:
void updateMaterials(int meshcount) {
if (meshrenderer.sharedMaterials.Length != meshcount) {
meshrenderer.sharedMaterials = new Material[meshcount];
}
int j = 0;
for (int i = 0; i < faces.Length; i++) { // faces: bool[6], true when face exists
if (faces*) {*
meshrenderer.sharedMaterials[j] = materials*;*
Debug.Log(j.ToString() + ": " + materials*.ToString());*
// Output:
// 0: Floor (UnityEngine.Material)
// 1: Wall-bricks (UnityEngine.Material)
j++;
}
}
}
Does anybody know what I am doing wrong here? Thanks.