# random colors for each triangle face

I’m trying to pull off an effect similar to this: my current strategy is to set the vertex color of each vertex of the triangle to the same color. Then randomizing that number for each triangle vertex group. I’ve already gotten pretty far with this

``````	void Start ()
{
_meshFilter = GetComponentInChildren<MeshFilter>();
setColorVertecies();
}

private void setColorVertecies()
{
Mesh mesh = _meshFilter.mesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
Color[] colors = new Color[vertices.Length];
Color triColor = getRandomColor();

//Debug.Log("numTri" + triangles.Length); //36, 42, 240
//Debug.Log("numVer" + vertices.Length); //24, 26, 55
for (int i = 0; i < triangles.Length; i++)
{
int vertIndex = triangles*;*
``````
• `````` 	if (i % 3 == 0)*
``````
• `````` 		triColor = getRandomColor();*
``````
• `````` 	colors[vertIndex] = triColor;*
``````
• `````` }*
``````
• `````` mesh.colors = colors;*
``````
• }*

• private Color getRandomColor()*

• {*

• `````` float red = Random.Range(0, 1.0f);*
``````
• `````` float green = Random.Range(0, 1.0f);*
``````
• `````` float blue = Random.Range(0, 1.0f);*
``````
• `````` return new Color(red, green, blue);*
``````
• }*
The problem is that some of the vertices are shared between triangles and the colors are getting interpolated. I figured out after some research that each vertex needs to be unique… aka not shared but I can’t figure out how to force my model to split vertices
Vertex color by face - Questions & Answers - Unity Discussions
I thought I was on the right track by setting the mesh import smooth angle to 0 but that didn’t seem to help at all:
Here’s a screen shot of the problem:
[7508-problem.jpg|7508]*

Is there a way to force unity to split vertices so each face can have its own solid color? Or am I on the wrong track and this effect would be easier to pull off using a shader or something?
update
Thanks @Eric5h5 and @robertbu that did the trick. I’d still like to figure out a export or import setting that could do this for me instead of doing it inside unity. This works great on simple models but I got some really complex geometry that the script is freezing on. Oh well I suppose that’s a problem for my 3D software and not necessarily Unity related.

Assuming you have the appropriate arrays that you got from the mesh called meshTriangles, meshVertices, etc., then this code will create unique vertices:

``````var newVertices = new Vector3[meshTriangles.Length];
var newUV = new Vector2[meshTriangles.Length];
var newNormals = new Vector3[meshTriangles.Length];
var newTriangles = new int[meshTriangles.Length];

// Rebuild mesh so that every triangle has unique vertices
for (var i = 0; i < meshTriangles.Length; i++) {
newVertices _= myVertices[meshTriangles*];*_
``````

newUV = meshUV[meshTriangles*];*
newNormals = meshNormals[meshTriangles*];*
_ newTriangles = i;
* }*
Then assign the newTriangles, newVertices, etc. arrays back to the mesh._