Why isnt this working?

So I am making a script for tiles and have applied it to an empty game object with a material attached.
However, the tilemap generates half the triangles and the rest are just gone. I would really appreciate it if someone could help.
Here is the script:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshRenderer))]
public class TileMap : MonoBehaviour {
int size_x = 100;
int size_z = 50;
float tilesize = 1.0f;

void Start()
{
    BuildMesh();
}
void BuildMesh()
{
    int numTiles = size_x * size_z;
    int numTriangles = numTiles * 2;

    int vsize_x = size_x + 1;
    int vsize_z = size_z + 1;
    int numVerts = vsize_x * vsize_z;

    Vector3[] vertices = new Vector3[numVerts];
    Vector3[] normals = new Vector3[numVerts];
    Vector2[] uv = new Vector2[numVerts];

    int[] triangles = new int[numTriangles * 3];

    int x, z;
    for (z = 0; z < vsize_z; z++)
    {
        for (x = 0; x < vsize_x; x++)
        {
            vertices[z * vsize_x + x] = new Vector3(x*tilesize,0,z*tilesize);
            normals[z * vsize_x + x] = Vector3.up;
            uv[z * vsize_x + x] = new Vector2((float) x/size_x, (float) z/size_z);
        }
    }

    for (z = 0; z < size_z; z++)
    {
        for (x = 0; x < size_x; x++)
        {
            int squareIndex = z * size_x + x;
            int triOffset = squareIndex * 6;
            triangles[triOffset + 0] = z * vsize_x + x + 0;
            triangles[triOffset + 2] = z * vsize_x + vsize_x + 1;
            triangles[triOffset + 1] = z * vsize_x + vsize_x + 0;

            triangles[triOffset + 3] = z * vsize_x + x + 0;
            triangles[triOffset + 5] = z * vsize_x + x + 1;
            triangles[triOffset + 4] = z * vsize_x + x + vsize_x + 1;
        }
    }

    Mesh mesh = new Mesh();
    mesh.vertices = vertices;
    mesh.triangles = triangles;
    mesh.normals = normals;
    mesh.uv = uv;

    MeshFilter mesh_filter = GetComponent<MeshFilter>();
    MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
    MeshCollider mesh_collider = GetComponent<MeshCollider>();

    mesh_filter.mesh = mesh;
}

}
Thanks in advance

So I changed this part of the code to:
for (z = 0; z < size_z; z++)
{
for (x = 0; x < size_x; x++)
{
int squareIndex = z * size_x + x;
int triOffset = squareIndex * 6;
triangles[triOffset + 0] = z * vsize_x + x + 0;
triangles[triOffset + 3] = z * vsize_x + vsize_x + 1;
triangles[triOffset + 2] = z * vsize_x + vsize_x + 0;

            triangles[triOffset + 4] = z * vsize_x + x + 0;
            triangles[triOffset + 5] = z * vsize_x + x + 1;
            triangles[triOffset + 4] = z * vsize_x + x + vsize_x + 1;
        }
    }

and it has perfectly fine overall tile, all flat and only seen from top and is working how i think it should be, but i fear it might do something to my code that i dont want.
Also could someone explain why that works?

Privacy & Terms