Out-of-bounds error for an explicit value of zero?

I’m relatively new to Unity, and am still coming to terms with the many subtle (and some quite emphatic) differences between Unity’s “Javascript” and ACTUAL Javascript.

The behavior of Arrays is particularly baffling. In the below code, for example, I am getting the error “Index is less than 0 or more than or equal to the list count” from the single line of code in the second for loop. I know it is this line because if I comment that line out, it doesn’t happen.

function removeTriangles() {
	var mesh : Mesh = GetComponent(MeshFilter).mesh;
	var vertices = Array(mesh.vertices);
	var triangles = Array(mesh.triangles);
	var trisToRemove = 3;
	for(var i = 0; i<triangles.length; i++) {
		if(i < trisToRemove*3) {
			vertices.RemoveAt(triangles*);*
  •  }*
    
  • }*
    _ for(var j = 0; j<trisToRemove*3; j++) {_
  •  triangles.RemoveAt(0);*
    
  • }*
  • mesh.triangles = triangles;*
  • mesh.vertices = vertices;*
    }
    As you can probably see, this function is simply designed to remove a set number of triangles from the mesh of a game object - first by referencing the triangles array for the indexes to remove from the vertices array, then removing these indexes from the triangles array so they match. Normally the trisToRemove variable would be set elsewhere in the code but I am explicitly declaring it here for demonstration purposes. I am using javascript arrays for the vertices and triangles as opposed to builtin arrays, because I am resizing them.
    Now… call me crazy, but how can an explicit value of zero (in triangles.removeAt(0)) be either ‘less that zero’ or ‘more than equal than the list count’? It makes absolutely no sense.
    Also confusing is the fact that, even if I comment out that line, I get a warning from the first use of .RemoveAt of an “Implicit downcast from ‘Object’ to ‘int’”.
    More confusing still… in spite of these errors, the function still does what it is intended to do. It removes 3 triangles from the game object’s mesh, which is immediately visible.
    Can anyone explain to me what the hell is going on here?

I guess your code is supposed to be:

//I siplified this a bit, you want the for loop with 2 conditions so it looks more natural to use both conditions in the for instead of adding an if inside
for(var i = 0; i < triangles.length && i < trisToRemove*3; i++) {
    vertices.RemoveAt(triangles*);*

}

//if you’re removing triangles then removing “trisToRemove*3” triangles is 3 times the amount of triangles you want (and for some values, more than the triangle count in your mesh probably)
for(var j = 0; j<trisToRemove; j++) {
triangles.RemoveAt(0);
}