When generating meshes from code, there are some strict rules to remember:
Don’t modify its data arrays directly, instead copy them to a temporary one, do the work, then re-assign it back to the mesh.
The vertices array must be assigned first, other arrays later. Once again, the number of vertices cannot exceed 65000.
Number of indices in the triangles array must equal to a triple of number of vertices.
Number of elements in the normals, tangents, colors and uv array must be equal to number of vertices, if assigned.