- Home /

# How to generate a subdivided plane mesh

Hello. I am trying to make a mesh that displays a grid of 16x16 vertices. I have already been successful at plotting the 256 vertices (I used gizmos to see that they are there). However, I seem to be having trouble with the triangles, which do not connect all of the vertices. Here is the code for my mesh; could someone please tell me what I'm doing wrong? Thanks! :)

```
public void RenderMesh()
{
var vertices = new List<Vector3>();
var uvs = new List<Vector2>();
var tangents = new List<Vector4>();
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
Vector3 position = new Vector3(x, 0, z);
vertices.Add(position);
uvs.Add(new Vector2((float) x / 16, (float) z / 16);
tangents.Add(new Vector4(1, 0, 1, -1));
}
}
var triangles = new List<int>();
for (int x = 0, vrt = 0; x < 15; x++)
{
for (int z = 0; z < 15; z++, vrt++)
{
triangles.Add(vrt);
triangles.Add(vrt + 16);
triangles.Add(vrt + 1);
triangles.Add(vrt + 16);
triangles.Add(vrt + 17);
triangles.Add(vrt + 1);
}
}
var mesh = new Mesh();
mesh.name = "Terrain";
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
mesh.tangents = tangents.ToArray();
mesh.RecalculateNormals();
_meshFilter.sharedMesh = mesh;
}
```

**Answer** by andrew-lukasik
·
Jul 21, 2021 at 09:23 PM

It was `vrt++`

fault. Calculate it as `y*numVerticesX + x;`

instead.

```
public void GenerateMesh ( int numVerticesX = 16 , int numVerticesZ = 16 )
{
var vertices = new List<Vector3>();
var normals = new List<Vector3>();
var uvs = new List<Vector2>();
for( int y=0 ; y<numVerticesZ ; y++ )
for( int x=0 ; x<numVerticesX ; x++ )
{
float tx = (float)x / (float)(numVerticesX-1);
float ty = (float)y / (float)(numVerticesZ-1);
vertices.Add( new Vector3(-0.5f+tx,0,-0.5f+ty) );
normals.Add( Vector3.up );
uvs.Add( new Vector2(tx,ty) );
}
var indices = new List<int>();
for( int y=0 ; y<(numVerticesZ-1) ; y++ )
for( int x=0 ; x<(numVerticesX-1) ; x++ )
{
int quad = y*numVerticesX + x;
indices.Add(quad);
indices.Add(quad+numVerticesX);
indices.Add(quad+numVerticesX+1);
indices.Add(quad);
indices.Add(quad+numVerticesX+1);
indices.Add(quad+1);
}
var mesh = new Mesh();
mesh.name = $"Terrain {mesh.GetHashCode()}";
mesh.vertices = vertices.ToArray();
mesh.triangles = indices.ToArray();
mesh.uv = uvs.ToArray();
mesh.normals = normals.ToArray();
_meshFilter.sharedMesh = mesh;
}
```

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

How does the Triangles work on Mesh? 1 Answer

Failed setting triangles in my mesh 1 Answer

How come do I keep getting this error on my voxel script 1 Answer

Create a Triangle Mesh. 0 Answers

Mesh adaption 1 Answer