# how to set multiple materials using array in the submesh of an object

I'm trying to insert multiple materials into the object's submesh using the array, but I'm not getting

public GameObject[] obj;

//material mat;

public void Color(Material mat) {

for (int x = 0; x < obj.Count; x++)
obj[x].GetComponent<MeshRenderer>().sharedMaterials[1] = mat;

Material[] mats = GetComponent<Renderer>().materials;
mats[1] = mat;
GetComponent<Renderer>().materials = mats;

{
print("New Material");
}
}

• Sorry, Can you be more specific of what you are trying to do?? – Shuvro Sarkar Feb 7 at 10:10
• Your second attempt here — capturing a mats array, setting its entry at a particular index, then assigning the modified array back to the renderer — this looks correct. Is it not performing the way you expect? If not, can you explain in detail what you want it to do, and what behaviour you're observing instead? – DMGregory Feb 7 at 13:29
• from the material line [] mats, it's only 1 material for the respective element, so I'm trying to insert an array of materials, but I'm not getting – Nitecki Feb 7 at 18:49
• Please try to explain that in more detail.The language barrier here is making it difficult to understand what you mean. Maybe try editing your question to give an example of what you want the materials array to contain before your code runs, then show an example of what you want the array to contain after your code has run. Then we can see clearly what change you're trying to make. – DMGregory Feb 8 at 3:24
• I'm sorry, see if you can understand now. – Nitecki Feb 8 at 4:15

material mat;
GetComponent<MeshRenderer> ().sharedMaterials [1] = mat;
Material[] mats = GetComponent<Renderer>().materials;
mats[1] = mat;
GetComponent<Renderer>().materials = mats;


First error: The class "Material" should be in capital letters. (your code shouldnt compile)

Second error:

GetComponent<MeshRenderer> ().sharedMaterials [1] = mat;


doesnt do anything. You can not assign that material like this.

The rest of your code looks correct.

1. Did you assign the "mat" in the inspector?
2. Does your mesh even have more than 1 submesh?
3. Do you even want to assign more than 1 material?
4. Whats the error ? Doesnt it compile? Null reference? Nothing happenes?
5. Does your mesh renderer get the materials or does it just look wrong in the 3d view?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaterialArraySetter : MonoBehaviour {

public Material Material1;

void Start ()
{
Material[] materialsArray = GetComponent<Renderer>().materials;
materialsArray[1] = Material1;
GetComponent<Renderer>().materials = materialsArray;

}

}


EDIT2:

You seem to have compiler errors.

• type is because I'm trying to use an array of gameobject or of materials either, so the object receives the color the script itself works like this, but with only one material settling on its due. material mat; GetComponent<MeshRenderer> ().sharedMaterials [1] = mat; Material[] mats = GetComponent<Renderer>().materials; mats[1] = mat; GetComponent<Renderer>().materials = mats; I'm trying to insert an array of materials, but I can not – Nitecki Feb 7 at 18:42
• i edited my answer – OC_RaizW Feb 8 at 0:14
• Do you really want to assign more than one material? is exactly that, I'm trying to set an array of material to assign several materials in the sub-mesh, but I'm not getting... here are the errors i.imgur.com/KIVAw8Y.png – Nitecki Feb 8 at 3:54
• i edited my answer - again – OC_RaizW Feb 8 at 11:02
• My God, I'm very blind, I swear to you that I saw a Length kkk, thank you very much. – Nitecki Feb 8 at 15:24