how to color a certain element .. [meshrenderer]

Hello, I am trying to color a certain element of a meshrenderer, but with .color, I made several methods, nothing else works

            method1
            for (int x = 0; x < obj.Length; x++)
            {
                Color color = obj[x].material.color;
                color[2] = redcolor;
                obj[x].material.color = color;
            }

            method2
            for (int x = 0; x < obj.Length; x++)
            obj[x].material[2].color = redcolor;

The name of the color variable in the shader is probably not “_Color”, which Material.color uses.

You’ll need to look at the material / shader you’re using and figure out the actual name, then use Material.SetColor.

It’s not just about coloring, I’m trying to insert a color in the specific element, the setmaterial and material.color works, but the problem I’m having and inserting that color in a specific element

What do you mean by “element”?

Perhaps you need to use Renderer.materials and target a material with a specific index on the MeshRenderer?

nothing no, it’s just a test I’m trying to do. the element is an int, so I’ll set a value there in the inspector without having to change it every time in the script

So I tried that too, but it works only with material and not with .color
but I haven’t found a solution yet

    public MeshRenderer[] obj;
    public Color redcolor;
  
    void Start()
    {
        for (int i = 0; i < obj.Length; i++)
        {
            Color colors = obj[i].material.color = redcolor;
            colors[2] = redcolor;
            obj[i].GetComponent<MeshRenderer>().sharedMaterial[2].color = colors;
        }

I dont know what you’re talking about. Can you describe what exactly you are trying to modify?

I was trying to change the color of a submesh, but I use getcomponent a lot, and for performance that’s bad, do you have any tips to help?

        if(index == 0)
        {
            for (int i = 0; i < obj.Length; i++)
                obj[i].GetComponent<MeshRenderer>().materials[2].color = redcolor;
        }
        else if(index == 1)
        {
            for (int i = 0; i < obj.Length; i++)
                obj[i].GetComponent<MeshRenderer>().materials[2].color = bluecolor;
        }
        else if(index == 2)
        {
            for (int i = 0; i < bloco.Length; i++)
                obj[i].GetComponent<MeshRenderer>().materials[2].color = greencolor;
        }
        //below has +30 indexes all with getcomponent '-'