# Color triangle on mesh

Hi, I’m trying to vertex color every triangle on mesh with random color, but I’m getting this:

[31148-wrong.jpg*_|31148]

but I’m want to get this:

[31149-correct.jpg*_|31149]

The red triangle is always one-colored and other ones on mesh aren’t.

``````using UnityEngine;
using System.Collections;

public void VertsColor(GameObject gameObject)
{
Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;

int[] triangles = mesh.triangles;
Vector3[] vertices = mesh.vertices;

Vector3[] verticesModified = new Vector3[triangles.Length];
int[] trianglesModified = new int[triangles.Length];

Color32 currentColor = new Color32();
Color32[] colors = new Color32[triangles.Length];

for (int i = 0; i < trianglesModified.Length; i++) {

// Makes every vertex unique
verticesModified _= vertices[triangles*];*_
``````

_ trianglesModified = i;
* // Every third vertex randomly chooses new color*
colors[triangles*] = currentColor;
if(triangles % 3 == 0)
currentColor = new Color(
Random.Range (0.0f, 1.0f),
Random.Range (0.0f, 1.0f),
Random.Range (0.0f, 1.0f),
1.0f);
}
// Applyes changes to mesh*

* mesh.vertices = verticesModified;
mesh.triangles = trianglesModified;
mesh.colors32 = colors;
}
Shouldn’t it be simply `if(i%3==0)`? And assign the color afterwards? i.e.: tri indexes 3,4,5 form a tri, so you pick a color for 3, use it for 3,4,5; then pick a new color just before assigning 6,7,8.