change color of the selected triangles inside human chest

hi,

How to change the color of triangles inside the human mesh.I have applied the following code to change the color of the chest inside the human mesh.
[32161-avataarcolor.jpg*_|32161]

void Update ()
{

    MeshFilter mf = GetComponent<MeshFilter>();

    if (mf == null) return;
    Mesh mesh = mf.mesh;
    int[] triangles = mesh.triangles;
    Vector3[] vertices = mesh.vertices;
    int[] trianglesNew = new int[triangles.Length];
    Vector3[] verticesNew = new Vector3[triangles.Length];
    for (int i = 0; i < trianglesNew.Length; i++)
    {
        Vector3 v3Pos = vertices[triangles*];*

trianglesNew = i;
verticesNew = v3Pos;
}
Color colorT = Color.red;
Color[] colors = new Color[trianglesNew.Length];
for (int i = 0; i < colors.Length; i++)
{
colorT = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1.0f);
colors = colorT;
}
mesh.vertices = verticesNew;
mesh.triangles = trianglesNew;
mesh.colors = colors;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
* }*
and i dont want to change the color of the whole material by specifying like this
renderer.material.color = color.green;
For eg:- i like to take the spine and shoulder left,shoulder right joints and there from i want to took some chest points and change the color of the chest triangles to colors like green or red.
and my doubt is whether mesh.colors= colors has any effect on the object that we are trying to change.
Please clarify my doubt…
Thanks
_*

First, making chnagres to the colors array is the way to change the color of individual triangles, BUT it only works is the shader used in the material for your mesh supports vertex colors. Only a few of the shaders that ship with Unity support vertex colors. So if you are not seeing color changes, then you probably are not using a shader that supports vertex colors.

But in looking at your code, I’m confused about a few other things. In changing colors, there is no reason to mess with the vertices of the mesh. For just changing the color of some triangles, you can cut your code down to:

void Update() {
    MeshFilter mf = GetComponent<MeshFilter>();
 
    if (mf == null) return;
    Mesh mesh = mf.mesh;
     
    Color[] colors = mesh.colors;
    for (int i = 0; i < colors.Length; i++)
    {
        colors *= new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1.0f);*

}

mesh.colors = colors;
}
Note that in general, the number of vertices in a mesh does not match the number of triangles in the triangle array. And that the colors array is the size of the vertices in the mesh, not the size of the triangles array. I know this is just a test, but if this was your real code, you would want to avoid the GetComponent() call every frame. You could get it once in Awake() or Start(). Also note that using the ‘new’ operator to construct a new colors array is fine in general, but since your goal here is to only color specific triangles, then your code will want a copy of the original colors as I’ve done here. In fact, you could get the colors array once in start and cache it, and then make changes and reassign as necessary.