How do you swap out the triangles in a mesh

I am generating my own procedural mesh. I use a 3 dimensional array that represents coordinates, and each coordinate is a vertex for the mesh. Then I draw triangles from vertex to vertex. Its pretty simple.

The problem is, I want to change out the triangles I draw with another combination of triangles. I can easily add more triangles in, but the original triangles will still be there.

I have tried to delete the original triangles like this:

function Update () {
if (Input.GetKeyDown ("q"))
{            
var verts = mesh.vertices;
var tris = mesh.triangles;
mesh.Clear();
tris = removeTriangle(7, tris);
mesh.vertices = verts;
mesh.triangles = tris;                   
}
} 

function removeTriangle(triangle : int, tris : int[]) : int[]{
for (var i = triangle*3; i < tris.Length-3; ++i) {
if (tris *== -1) break;*

tris = tris[i+3];
}
return tris;
}
In the above example, The code would delete the 7th triangle. Seems easy, right? Unfortunately, the array that represents the mesh’s triangles changes all the values above 7 down one, making a new “7th” triangle. Because of this, I have no way of controlling my triangles.
So my question is, does anyone else have a method to control the mesh.triangles array so that when I create a triangle, I can easily delete it at will. Or, if you know how, do you have a different method of deleting triangles?

Thank you whydoidoit, your code helped me figure it out. I can successfully make a triangle disappear without “deleting” it from the triangles.mesh array. Here is my code for anyone curious:

function Update () {
if (Input.GetKeyDown ("q"))
{            
var verts = mesh.vertices;
var tris = mesh.triangles;
mesh.Clear();
tris = removeTriangle(7, tris);
mesh.vertices = verts;
mesh.triangles = tris;                   
}
} 
 
function removeTriangle(triangle : int, tris : int[]) : int[]{
for (var i = triangle*3; i < tris.Length-3; ++i) {
if (tris *== -1) break;*

tris = tris[i+3];
}
return tris;
}
The code above will remove the 7th triangle in a mesh whenever you press “q”. The best part is, I can also input a triangle back in with a few tweaks of the code above, which is just what I need. So now I can basically “edit” any triangle I want!

You need to pass a smaller array if you want any triangles to disappear - at the moment you are just duplicating the last triangle every time.

function removeTriangle(triangle : int, tris : int[]) : int[]{
   var result = new int[tris.Length-3];
   for (var i = 0, j=0; i < (tris.Length/3)-1; i++,j++) {
         if(i==triangle) j++;
         result[(i*3)+0] = result[(j*3)+0];
         result[(i*3)+1] = result[(j*3)+1];
         result[(i*3)+2] = result[(j*3)+2];

   }
   return result;
}

Just thought I’d add something as this thread was very useful to me.

The following will actually remove any number of triangles in the toBeDeleted index array for good (which is what I need).

int[] RemoveTriangle(int[] toBeDeleted, int[] triangles){

	// New triangle array
	int[] newTriangles = new int[triangles.Length - (toBeDeleted.Length*3)];

	for (int i = 0, j = 0; i < newTriangles.Length/3; ++i, ++j) {

		int start = j;

		// flag if this triangle is to be deleted
		bool found = false;

		//Look ahead until you find a triangle to be preserved
		for (int k = start; k < triangles.Length; ++k) {

			found = false;

			// If you get to a triangle to be deleted, move forward 1 
			for (int l = 0; l < toBeDeleted.Length; ++l){
				if(j == toBeDeleted[l]){
					j+=1;
					found = true;
					break;
				}
			}

			// If a triangle is not found in the toBeDeleted array, break 
			if(!found)
				break;
		}

		// copy data to new array
		newTriangles[(i*3) + 0] = triangles[(j*3) + 0];
		newTriangles[(i*3) + 1] = triangles[(j*3) + 1];
		newTriangles[(i*3) + 2] = triangles[(j*3) + 2];
	}
	return newTriangles;
}