The first triangle is always the hardest, the rest are easy......

To attempt to get a triangle showing procedurally, I did the following.

Start a new instance of Unity.
Create a new GameObject in the hierarchy to hold the triangle.
Added a MeshFilter and MeshRenderer to the GameObject.
Created a C# Script in Assets and added this code to the Start function:

Mesh mesh = newMesh();

mesh.vertices = newVector3[3];

mesh.vertices[0] = newVector3(0, 0, 0);

mesh.vertices[1] = newVector3(0, 1, 0);

mesh.vertices[2] = newVector3(1, 0, 0);

mesh.normals = newVector3[3];

mesh.normals[0] = newVector3(0, 0, -1);

mesh.normals[1] = newVector3(0, 0, -1);

mesh.normals[2] = newVector3(0, 0, -1);

mesh.uv = newVector2[3];

mesh.uv[0] = newVector2(0, 0);

mesh.uv[1] = newVector2(0, 1);

mesh.uv[2] = newVector2(1, 0);

mesh.triangles = newint[3];

mesh.triangles[0] = 0;

mesh.triangles[1] = 1;

mesh.triangles[2] = 2;

//mesh.RecalculateNormals();

mesh.RecalculateBounds();

MeshFilter filter = GetComponent();

if (filter != null)

{

filter.sharedMesh = mesh;

}

Dragged the script onto the GameObject to hook it up.
Pressed play.
No triangle. Where is the $%^& triangle.

You need to assign a whole array. You cant just edit the elements in place. Like you cant edit transform.position.x directly.

1 Like

Yup. This type of code works: mesh.triangles=triangles;
I got my triangle up…

Yup. A normal array is passes by reference. But the mesh class passes it’s arrays by value. So when you access mesh.triangles[0] it’s actually creating a new copy of the triangles array.

The solution, as pointed out by @ThermalFusion , is to make all of the changes to the array before assigning it.

1 Like