- Home /
Assigning Triangles from String Array issue
Hi Unity Community
Probably a pretty simple issue. I am trying to parse the string values from an input field to define a mesh, and while I have the UV and vertex variations working fine, I can't see to complete the functionality for assigning triangles.
The current issue here is the script is throwing me a 'Array index is out of range' error, and no matter what I change in the array assigning process I can’t seem to resolve this. Not only does it not work but it also causes an issue with accessing the Mesh Filter of the game object the user input is meant to updating and disables the other toggles’ functionality from working. I’m quite new to the splitting of strings so I’m sure this is a simple mistake; any guidance would be very much appreciated!
Here is my code:
function ExpVertText(userText : String) // for importing verts back to Mesh from input field
{
var textArray = userText.Substring (1, userText.Length-2).Split ([")("], System.StringSplitOptions.RemoveEmptyEntries);
var curObj = GameObject.Find("TestObject"); //current object
var curMesh = curObj.GetComponent(MeshFilter).mesh;
//**Vertice Import**
var vertToggle = GameObject.Find("VertToggle").GetComponent(Toggle);
var curVertArray : Vector3[] = curObj.GetComponent(MeshFilter).mesh.vertices; //current verts
var vectArray = new Vector3[textArray.Length];//new verts
//**UV Import**
var uvToggle = GameObject.Find("UVToggle").GetComponent(Toggle);
var curUVArray : Vector2[] = curObj.GetComponent(MeshFilter).mesh.uv; //current UV's
var uvArray = new Vector2[textArray.Length];//new UV's
//**Tri Import**
var triToggle = GameObject.Find("TriToggle").GetComponent(Toggle);
var curTriArray : int[] = curObj.GetComponent(MeshFilter).mesh.triangles; //current Tri's
var triArray = new int[textArray.Length];//new Tri's
var triTextArray : String[]; // holds string tri vals from text input
var triTextInts : int[]; //holds Tri int values
//**Issue Section** Tri Update
triTextArray = userText.Split(""[0]); //string to string array for ea. value
for (var m = 0; m < triTextArray.Length; m++) {
if (triToggle.isOn)
{
triTextInts[m] = parseInt(triTextArray[m]); // string array parsed to int array
Debug.Log( "Stored triTextArray: " + triTextInts[m]);
Debug.Log("Old Tri: " + curTriArray[m]);
curTriArray[m] = triTextInts[m];
Debug.Log("New Tri: " + curTriArray[m]);
curObj.GetComponent(MeshFilter).mesh.triangles = curTriArray;
Debug.Log("New Tri - Mesh Updated");
}
}
//***Store Values***
for (var i = 0; i < textArray.Length; i++) {
var numbers = textArray[i].Split(","[0]);
if (vertToggle.isOn)
{
vectArray[i] = Vector3(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]));
}
if (uvToggle.isOn)
{
uvArray[i] = Vector2(parseFloat(numbers[0]), parseFloat(numbers[1]));
}
}
//***Vert Assign***
if (vertToggle.isOn)
{
for (var j = 0; j < vectArray.Length; j++)
{
Debug.Log("Old vert: " + curVertArray[j]);
curVertArray[j] = vectArray[j];
Debug.Log("New vert: " + curVertArray[j]);
curObj.GetComponent(MeshFilter).mesh.vertices = curVertArray;
Debug.Log("New Verts - Mesh Updated");
}
}
//***UV Assign***
if (uvToggle.isOn)
{
for (var k = 0; k < uvArray.Length; k++)
{
Debug.Log("Old UV: " + curUVArray[k]);
curUVArray[k] = uvArray[k];
Debug.Log("New UV: " + curUVArray[k]);
curObj.GetComponent(MeshFilter).mesh.uv = curUVArray;
Debug.Log("New UV's - Mesh Updated");
}
}
return curObj;
}