procedural mesh(re)making, debug help

hello. i am new here, and my problem is probably a beginner problem but it is driving me crazy. my game idea needed a way to procedurally split objects(meshes), CSG boolean substraction would cut it but i am stuck at the beginning. here is the script:

function Start () {

var mesh : Mesh = GetComponent(MeshFilter).mesh;

var newvertices = new Vector3[mesh.triangles.Length];
var newtriangles = new int[mesh.triangles.Length];

for (v = 0; v < mesh.triangles.Length; v++) 
	{
		newvertices[v] = mesh.vertices[mesh.triangles[v]];
		newtriangles[v] = i;
	}

//-----------------------------------------------------------------------------------------------------
var lenght : int = 0;
var c : int = 0;

for (l = 0; l < newtriangles.Length; l += 3) 
{
	if (newvertices[newtriangles[l].y < 0 || newvertices[newtriangles[l+1].y < 0 || newvertices[newtriangles[l+1].y < 0)
		{
			lenght += 3;
		}
}

var vertices = new Vector3[lenght];
var triangles = new int[lenght];


for (i = 0; i < newtriangles.Length; i += 3) 
{
	if (newvertices[newtriangles[i].y < 0 || newvertices[newtriangles[i+1].y < 0 || newvertices[newtriangles[i+1].y < 0)
		{
			vertices[c] = newvertices[newtriangles[i];
			vertices[c+1] = newvertices[newtriangles[i+1];
			vertices[c+2] = newvertices[newtriangles[i+2];
			triangles[c] = newtriangles[i];
			triangles[c+1] = newtriangles[i+1];
			triangles[c+2] = newtriangles[i+2];
			c += 3;
		}
}
//------------------------------------------------------------------------------------------------------------------------

mesh.vertices = vertices;
mesh.triangles = triangles;

var uvs : Vector2[] = new Vector2[mesh.vertices.Length];
for (var u = 0 ; u < uvs.Length; u++)
uvs[u] = Vector2 (mesh.vertices[u].x, mesh.vertices[u].z);
mesh.uv = uvs;
mesh.uv2 = uvs;

mesh.RecalculateBounds();
mesh.RecalculateNormals();
}

basically, it is supposed to cut upper part of the mesh polygons at zero plane. first it copies vertices and rebuilds the triangle array so that every polygon would have 3 of its own. then comes the part of the code(between comment sections) which is supposed to first find the length of the new mesh arrays, declare them, and then fill them with “good” vertices and triangles entry’s. unity reports that errors are in the if conditionals by which vertices are iterated:

  • Finished compile Library/ScriptAssemblies/Assembly-UnityScript.dll
    Assets/backup.js(20,124): BCE0043: Unexpected token: ).

(Filename: Assets/backup.js Line: 20)

Assets/backup.js(26,35): BCE0043: Unexpected token: ;.

(Filename: Assets/backup.js Line: 26)

Assets/backup.js(27,32): BCE0043: Unexpected token: ;.

(Filename: Assets/backup.js Line: 27)

Assets/backup.js(32,124): BCE0043: Unexpected token: ).

(Filename: Assets/backup.js Line: 32)

Assets/backup.js(34,37): BCE0043: Unexpected token: =.

(Filename: Assets/backup.js Line: 34)

Assets/backup.js(34,66): BCE0044: expecting ), found ‘;’.

(Filename: Assets/backup.js Line: 34)

Assets/backup.js(35,70): BCE0043: Unexpected token: ;.

(Filename: Assets/backup.js Line: 35)

Assets/backup.js(36,70): BCE0043: Unexpected token: ;.

(Filename: Assets/backup.js Line: 36)

Assets/backup.js(56,1): BCE0044: expecting EOF, found ‘}’.

(Filename: Assets/backup.js Line: 56)

dont know what to do…any help i appreciated. thanks.

3 of your for loops don’t declare the counter variable; they are missing the “var” at the start. (though, this may be another “time saver” from JavaScript)

for (v = 0; v < mesh.triangles.Length; v++)
for (l = 0; l < newtriangles.Length; l += 3)
for (i = 0; i < newtriangles.Length; i += 3)

Change to:

for (var v = 0; v < mesh.triangles.Length; v++)
for (var l = 0; l < newtriangles.Length; l += 3)
for (var i = 0; i < newtriangles.Length; i += 3)

Beyond that, I can’t see anything else immediately obvious to me. FYI, you typo’d your “lenght” variable. :slight_smile:

Is there any more code in that backup.js file beyond what you posted?

You’re missing brackets…you repeatedly have two "["s but only one “]”. And no, you don’t need “var” in JS, unless you use #pragma strict.

–Eric

haha, i know, i do that often. when i turned it to “length” it went blue so i thought it is better. tried declaring counters but the same errors remain. i found a new one also which i would get when i would run it clean:

	if (newvertices[newtriangles[i].y < 0 || newvertices[newtriangles[i+1].y < 0 || newvertices[newtriangles[i+1].y < 0)
		{
			vertices[c] = newvertices[newtriangles[i];
			vertices[c+1] = newvertices[newtriangles[i+1];
			vertices[c+2] = newvertices[newtriangles[i+2];
			triangles[c] = c;
			triangles[c+1] = c+1;
			triangles[c+2] = c+2;
			c += 3;
		}

would get out of bounds for triangle array because i remade same triangle array instead of putting “c” as index.

i am having one of those moments when you just bang your head an scream stupid. thank you! anyway, it works now.