# Triangle Mesh Problem

I'm having a problem with creating a circular mesh I created two scripts:

``````static function Make(names,design,vert,line)  //This creates a mesh
{
var i;
var j;
var newObject : GameObject = new GameObject(names);
var newMesh : Mesh = new Mesh();
newMesh.vertices=vert;
var uvs=new Vector2[newMesh.vertices.length];
for(i=0;i<uvs.Length;i++)
{
uvs<em>=Vector2(newMesh.vertices_.x,newMesh.vertices*.z);*_</em>
<em>_*}*_</em>
<em>_*newMesh.uv=uvs;*_</em>
<em>_*newMesh.triangles=line;*_</em>
<em>_*newMesh.RecalculateNormals();*_</em>
<em>_*newObject.GetComponent(MeshFilter).mesh=newMesh;*_</em>
<em>_*if(design)*_</em>
<em>_*{*_</em>
<em>_*}*_</em>
<em>_*return 0;*_</em>
<em>_*}*_</em>
<em><em>*___*</em></em>
<em>_*var fire : Texture2D;*_</em>
<em>_*var vertex=new Array();*_</em>
<em>_*var lines=new Array();*_</em>
<em>_*var names="beta";*_</em>
<em>_*var newObj;*_</em>
<em>_*var i=0.0;*_</em>
<em>_*var ang=0.0;*_</em>
<em>_*var num=30;*_</em>
<em>_*function Start ()    //Make an approximate circle with 30 sides and a radius of 1*_</em>
<em>_*{*_</em>
<em>_*vertex.Push(Vector3(0,1,0));*_</em>
<em>_*for(i=1;i<num+1;i++)*_</em>
<em>_*{*_</em>
<em><em>_ang=i/num*2*3.1415926535897932384626433832795028841971693993751058209749445923078164;_</em></em>
<em>_*if(i==num)*_</em>
<em>_*{*_</em>
<em>_*lines.Push(0);*_</em>
<em>_*lines.Push(num);*_</em>
<em>_*lines.Push(1);*_</em>
<em>_*}*_</em>
<em>_*else*_</em>
<em>_*{*_</em>
<em>_*lines.Push(0);*_</em>
<em>_*lines.Push(i);*_</em>
<em>_*lines.Push(i+1);*_</em>
<em>_*}*_</em>
<em>_*}*_</em>
<em>_*Triangles.Make(names,fire,vertex,lines); //This calls upon the above function*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*<hr>*_</em>
<em>_*<p>This doesn't work, the circle is not made. I'm not sure why but the error that Unity gives is:</p>*_</em>
<em>_*<p>InvalidCasatException: Cannot cast from source type to destination type.</p>*_</em>
<em>_*<p>I'm not sure what this means.</p>*_</em>

A few hints:

var i;

var j;

This is rarely a good idea, because those are dynamically typed. Either define the type explicitly or through type inference. Same deal with this:

static function Make(names,design,vert,line)

Define the types, otherwise the code often ends up doing things you don't want (plus it's really slow).

3.1415926535897932384626433832795028841971693993751058209749445923078164;

There's no point to using so many digits...one, floats don't have anywhere near that level of precision, and two, you can just use Mathf.PI instead.

Also:

var vertex=new Array();

Using Array is generally pretty useless these days. If you know or can calculate the size of the array ahead of time, use a Vector3[] array, or else if you need a dynamically-sized array, use `List.()`, which is much faster than Array (and not too much slower than a Vector3[] array), and not dynamically typed.

Finally, many of your variables are global (defined outside the function) for no apparent reason; they should generally be local to the function unless it's actually necessary for them to be accessed by other functions.

Most of the values in lines end up being floats, because you used this:

``````var i=0.0;

``````

You can't use floats to construct the triangles array. Change it to

``````var i=0;

``````