If you have N
submeshes, the renderer for those meshes will need to have N
materials.
Many tutorials focus on renderers with only one material, which you access through its material
property. If you have multiple materials, you can instead access the materials
array. Each material n
corresponds to submesh n
.
If you want, you can apply different materials to each submesh by editing your assets in the inspector. Each material could have different textures, different instance parameters, or so on.
Or, you could apply one material to all of them, and then edit that material in a script. Bear in mind that Unity uses a copy-on-access pattern, so it will internally create N
new material instances the first time you access the materials
array.
As an example, the following would apply a random color to each submesh:
void Start() {
Material[] materials = renderer.materials;
for (int i=0; i<materials.Length; i++) {
float r = Random.value;
float g = Random.value;
float b = Random.value;
materials[i].color = new Color(r, g, b, 1f);
}
}