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();
    var uvs=new Vector2[newMesh.vertices.length];
 <em>_*return 0;*_</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 rad=1.0;*_</em>
<em>_*var num=30;*_</em>
<em>_*function Start ()    //Make an approximate circle with 30 sides and a radius of 1*_</em>
 <em>_*Triangles.Make(names,fire,vertex,lines); //This calls upon the above function*_</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>
<em>_*<p>Thanks for your help.</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).


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


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;