Make a square at runtime by making a new mesh?

Hello, all. I was wondering if there was a way to make a square mesh at runtime in JS. I found the example in the script refrence but it draws a triangle, so when I change (for tris) [0, 1, 2] to [0, 1, 2, 3], it gives me the error that the number of tris has to be a multiple of three. How can I make it so that I get a square strictly from code(I don’t want to assign the mesh). All help is appreciated.

A quad are two adjacent triangles, so use [0,1,2,0,2,3] for your tris (assuming you already added the 4th vertex to the array in that example. Also, the 4 vertices should either be in clockwise or counter-clockwise order, otherwise this tris array will connect the wrong vertices)

All meshes are made up of triangles. You have to give in 6 indices to build up 2 triangles with 4 vertices.

The vertices have to be in clockwise order.

tris[0] = 0;
tris[1] = 1;
tris[2] = 2;

tris[3] = 2;
tris[4] = 3;
tris[5] = 0;

Another way would be to setup a trianglestrip but you have to watch the order of the triangles. To set a triangleStrip index-array you have to use the Mesh.SetTriangleStrip() instead of assigning your array to Mesh.triangles or using Mesh.SetTriangles(). Unfortunately the SetTriangleStrip function isn’t documented yet (at least i don’t find it in the scripting ref.) but it works, i used it already :wink: