Why when i loop over the vertices it's not showing the mesh ?

I have two functions in the script that i generate the vertices the triangles normals and uv. If i call in the start function to the function Generate it will work fine and will show a mesh with two triangles. I can change the mesh size using the variables width and height.

But now i want to use the function GenerateOrigin.
So if i set for example the width and height both to 16. Then when creating the vertices array i have 298 vertices.
Then i loop over them. The problem is i don’t know how to loop over the triangles and normals and uv. Using GenerateOrigin i want first to see only 289 points when running the game. 289 vertices.

Then somehow i want to make in each 4 vertices two triangles.

But i don’t see anything when using the GenerateOrigin. Even if i set only the vertices in to the mesh: mesh.vertices = vertices; i don’t see the vertices. Why i can’t see the vertices when using the GenerateOrigin ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grid : MonoBehaviour
{
    public int width;
    public int height;

    private MeshFilter meshf;
    private Mesh mesh;

    private void Start()
    {
        meshf = GetComponent<MeshFilter>();
        mesh = new Mesh();
        meshf.mesh = mesh;

        Generate(); 
    }

    private void GenerateOrigin()
    {
        //Vertices
        
        Vector3[] vertices = new Vector3[(width + 1) * (height + 1)];
        
            for (int i = 0, y = 0; y <= height; y++)
            {
                for (int x = 0; x <= width; x++, i++)
                {
                    vertices *= new Vector3(x, y);*

}
}

//Triangles

int[] tri = new int[6];
{
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;

tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
}

//Normals (Display object and set direction)

Vector3[] normals = new Vector3[(width + 1) * (height + 1)];
{
for (int i = 0; i < normals.Length; i++)
{
normals = Vector3.forward;
}
}

//UVs (How textue will display)

Vector2[] uv = new Vector2[(width + 1) * (height + 1)];
{
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
}

//Assgind Arrays

mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
}

private void Generate()
{
//Vertices

Vector3[] vertices = new Vector3[4];
{
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(width, 0, 0);
vertices[2] = new Vector3(0, height, 0);
vertices[3] = new Vector3(width, height, 0);
}

//Triangles

int[] tri = new int[6];
{
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;

tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
}

//Normals (Display object and set direction)

Vector3[] normals = new Vector3[4];
{
normals[1] = Vector3.forward;
normals[2] = Vector3.forward;
normals[3] = Vector3.forward;
}

//UVs (How textue will display)

Vector2[] uv = new Vector2[4];
{
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
}

//Assgind Arrays

mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
}
}

First of all you should call RecalculateBounds after you assigned all your vertex / triangle information.

Next the way you create your Mesh object you should assign it to “sharedMesh” and not “mesh”.

In your “Generate” example your normals are pointing in the wrong direction. Your two triangles are only visible from -forward.

When you dynamically create your vertices you just have to reference the correct vertices. in order to generate the triangle indices. Since you did not duplicate any vertices your mesh would use shared vertices in between the quads. So the indices for one quad are:

int w = (width + 1);
int i0 = x   + y*w
int i1 = x+1 + y*w
int i2 = x   + y*w + w
int i3 = x+1 + y*w + w

x and y would refer to the quad position, not the vertices. So it’s one less than the vertex indices. Those 4 vertex indices would be used to create two triangles:

i0, i2, i1
i0, i1, i3

for example.