Procedural mesh creation - How?

Hi people, I’m trying to script something that’s way over my capacities, therefor I need to procedurally generate a mesh. The only problem I have is that I really don’t get it how triangles are to be scripted, since the documentation seems a bit unclear about this.

When I got five vertices, let’s call them pt0 - pt4, then how am i supposed to draw a poly between pt0, pt1, pt2 and another one between pt2, pt3, pt4?
This is what I’ve tried so far:

var pt : Vector3[]; //This including the five points pt0-pt4.
function Update () {
  mesh.vertices = pt;
  mesh.triangles = [0, 1, 2];
  mesh.triangles = [2, 3, 4];
}

I don’t know why, but I’m not able to draw the polys where I want them. Sometimes, they seem to be drawn randomly or without a logical order. Can anyone tell me what’s wrong with this? Or how Triangles are to be created procedurally?

You have to assign the entire triangles array all at once. The length of the array has to be a multiple of 3, because what it describes, is a connect-the-dots between vertices. Every time you do mesh.triangles =, that’s a new mesh you’re dealing with. Not in the sense of a new instance of the Mesh class, but in terms of it being a new set of triangles that make up a surface.

mesh.triangles = [0,1,2, 2,3,4];

Thanks, Jessy, that should do it :slight_smile: