# Read Vertices in a specific order?

I need to read the vertices of a mesh in order from left to right, then backwards to forwards, but the order the vertices are read in are incredibly awkward. They seem to read clockwise around the edges then spiraling in towards the center. Sorry if it's hard to understand what i'm asking for, i've included a picture of the results i'm currently getting so hopefully that can help. My current code is listed below.

```
//There is a "public int intensity" at the header to adjust the intensity (obviously) of the generation
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int i = 0;
int step = 0;
float lastY = 0;
while (i < vertices.Length)
{
vertices[i] += new Vector3(vertices[i].x, Random.Range(lastY + -1,lastY + 1) * intensity, vertices[i].z);
lastY = vertices[i].y;
i++;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
transform.gameObject.AddComponent<MeshCollider>();
```

**Answer** by JonHUnity
·
Oct 25, 2016 at 12:38 PM

The order of the vertices in a Mesh are determined by the order they were assigned when created. Unless explicitly documented by your importer or by the code which generated your mesh, there are no guarantees about the order of the vertices in the array.

Your picture looks like your mesh is a procedurally generated flat plane. To accomplish this with a specific order to the vertices, I recommend generating the mesh yourself. For example, to create a grid-based mesh, you can do something like the following:

```
public class CreateMesh : MonoBehaviour {
const int width = 100;
const int height = 100;
// Generate the mesh with a well-known vertex order
void Start () {
Mesh mesh = gameObject.AddComponent<MeshFilter>().mesh;
gameObject.AddComponent<MeshRenderer>();
mesh.vertices = GenerateVertices(width, height);
mesh.triangles = GenerateTriangles(width, height);
}
private int[] GenerateTriangles(int width, int height)
{
//generate two triangles per vertex except the last column and last row
int[] triangles = new int[(width - 1) * (height - 1) * 6];
for (int y = 0; y < height - 1; y++ )
{
for (int x = 0; x < width - 1; x++ )
{
triangles[(y * (width - 1) + x) * 6 ] = y * width + x;
triangles[(y * (width - 1) + x) * 6 + 1] = y * width + x + 1;
triangles[(y * (width - 1) + x) * 6 + 2] = y * width + x + 1 + width;
triangles[(y * (width - 1) + x) * 6 + 3] = y * width + x;
triangles[(y * (width - 1) + x) * 6 + 4] = y * width + x + 1 + width;
triangles[(y * (width - 1) + x) * 6 + 5] = y * width + x + width;
}
}
return triangles;
}
Vector3[] GenerateVertices(int width, int height)
{
Vector3[] vertices = new Vector3[width * height];
for (int y = 0; y < height; y++ )
{
for (int x = 0; x < width; x++ )
{
vertices[y * width + x] = new Vector3(x / (float)width, y / (float) height);
}
}
return vertices;
}
void Update () {
Mesh mesh = this.gameObject.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
SetVertex(vertices, 10, 12, .55f);//set the z component of the vertex at x=10, y=12 to .55
mesh.vertices = vertices;
}
private void SetVertex(Vector3[] verts, int x, int y, float z)
{
verts[width * y + x].z = z;
}
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.