Unity generates two Vertecies instead of one

So I used the Brackeys mesh generation tutorial as reference since I didnt work with meshes in a while and I stumbled upon a bug that I dont understand why it is there. As you can see I increment my verts value by one after finishing the inner loop but for some reason my code still generates polygons when switching lines. Surely I am overseeing something here but after trying to trouble shoot it still happens and I think I need a second opinion on my code

//Generate mesh
void GenerateMesh()
    {
        triangles = new int[size * size * 6];
        vertices = new Vector3[(size + 1) * (size + 1)];

        for(int y = 0, i = 0; y < size; y++)
        {
            for(int x = 0; x < size; x++)
            {
                vertices[i] = new Vector3(x, noise[x, y] * 15, y);
                i++;
            }
        }

        int verts = 0;
        int tris = 0;

        for(int i = 0; i < size - 1; i++)
        {
            for(int j = 0; j < size - 1;j++)
            {
                triangles[tris + 0] = verts + 0;
                triangles[tris + 1] = verts + size + 1;
                triangles[tris + 2] = verts + 1;
                triangles[tris + 3] = verts + 1;
                triangles[tris + 4] = verts + size + 1;
                triangles[tris + 5] = verts + size + 2;

                tris += 6;
                verts++;
            }
            verts++;
        }

    }

//Show mesh
    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices;
        mesh.triangles = triangles;
       
        mesh.RecalculateNormals();
    }

Here you see the result (look from below):


Otherwise it works as intended

Cut it way down to perhaps 3 x 3 verts and 2x2 (x2) triangles so you can debug what’s going on by printing out indices and scribbling up a quick diagram of vert numbers, triangle numbers, etc.

Also remove the noise term and make it zero so the mesh is completely flat.

Get the basic bookkeeping for the grid mesh generation bulletproof solid first, THEN start layering interesting things on it.

1 Like

You generate not enough vertices as you stop your x and y loop at size-1 but you need to go up to size.

1 Like