triangles colors render on top of each other

I am using a script to color my mesh triangles. The works but I get incorrect rendering. Any idea what is causing the problem?

void Start () {
        Mesh mesh = gameObject.GetComponent<MeshFilter>().sharedMesh;
        int[] triangles = mesh.triangles;
        Vector3[] vertices = mesh.vertices;
        Vector2[] UVs = mesh.uv;

        Color[] colors = new Color[triangles.Length];

        Vector3[] modifiedVerts = new Vector3[triangles.Length];
        Vector2[] newUVs = new Vector2[triangles.Length];
        int[] modifiedtris = new int[triangles.Length];

        Color c = new Color();
        for(int i = 0; i < triangles.Length; i++)
        {
            modifiedVerts _= vertices[triangles*];*_

modifiedtris = i;
newUVs = UVs[triangles*];*

int index = triangles*;*
if (i % 3 == 0)
{
c = new Color(Random.Range(0.2f, 0.7f),
Random.Range(0.2f, 0.7f),
Random.Range(0.2f, 0.7f),
1.0f);

}
colors[index] = c;
}

mesh.vertices = modifiedVerts;
mesh.triangles = modifiedtris;
mesh.uv = newUVs;

mesh.RecalculateNormals();[97090-pervertex.png|97090]*
mesh.RecalculateBounds();
mesh.RecalculateTangents();_

mesh.colors = colors;

}

_*

You most likely use a shader that doesn’t write to the depth buffer and / or doesn’t perform a depth test.

The triangles in one mesh are rendered in the order they are defined in the triangles array. It looks like one of the top triangles is rendered first (the area that is approximately covered by the red and brown triangle). This however is completely overwritten. Next the red front face is rendered which overwrites the top color in the back. Next the top lower half is rendered (green), that is again overwritten by brown, blue and magenta as they are rendered later.

If depth write and depth testing is used by the shader a triangle which is rendered further away can’t overwrite an already rendered triangle that is closer due to the depth test.

It also seems your shader does not use any culling (so front and back side of each triangle are rendered). It would help to know what kind of cube / mesh you want to render? Should that cube be opaque or transparent?

Most transparent shaders do not write to the depth buffer but do perform a depth test. Unity always renders opaque geometry first. Transparent objects are sorted from far to near. However self-overlapping triangles within a single mesh are not reordered. So a transparent mesh should never have triangles that self-overlap. This is the typical example which can’t be solved by sorting, only by splitting at least one triangle into several smaller triangles:
example

For more information on that problem, see this article.

edit

Here’s a simple shader that combines a texture (if provided) with the vertex colors of the mesh:

//filename: UnlitVertexColor.shader
Shader "Unlit/UnlitVertexColor"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float4 color : COLOR0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
				float4 color : COLOR0;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.color = v.color;
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				return i.color + col;
			}
			ENDCG
		}
	}
}

If you don’t want to use any texture at all you can simplify even more.

Here’s the same thing without any texture, just plain unlit vertex colors:

Shader "Unlit/UnlitVertexColor"
{
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float4 color : COLOR0;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float4 color : COLOR0;
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.color = v.color;
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				return i.color;
			}
			ENDCG
		}
	}
}

Alternatively if you don’t want to do any fancy things in the shader you can use an old fix-function shader without any CG code. This should do the same as the last example:

Shader "Unlit/UnlitVertexColor"
{
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		Pass
		{
			Cull Back
			ZTest LEqual
			ZWrite On
			Lighting Off
			BindChannels
			{
				Bind "Vertex", vertex
				Bind "texcoord", texcoord
				Bind "Color", color
			}
		}
	}
}