- Home /
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(); newObject.AddComponent(MeshFilter); newObject.AddComponent(MeshRenderer); newMesh.vertices=vert; var uvs=new Vector2[newMesh.vertices.length]; for(i=0;i<uvs.Length;i++) { uvs[i]=Vector2(newMesh.vertices[i].x,newMesh.vertices[i].z); } newMesh.uv=uvs; newMesh.triangles=line; newMesh.RecalculateNormals(); newObject.GetComponent(MeshFilter).mesh=newMesh; if(design) { newObject.AddComponent(MeshCollider); } return 0; }
var fire : Texture2D; var vertex=new Array(); var lines=new Array(); var names="beta"; var newObj;
var i=0.0; var ang=0.0; var rad=1.0; var num=30; function Start () //Make an approximate circle with 30 sides and a radius of 1 { vertex.Push(Vector3(0,1,0)); for(i=1;i<num+1;i++) { ang=i/num*2*3.1415926535897932384626433832795028841971693993751058209749445923078164; vertex.Push(Vector3(Mathf.Sin(ang)*rad,1,Mathf.Cos(ang)*rad)); if(i==num) { lines.Push(0); lines.Push(num); lines.Push(1); } else { lines.Push(0); lines.Push(i); lines.Push(i+1); } } Triangles.Make(names,fire,vertex,lines); //This calls upon the above function }
This doesn't work, the circle is not made. I'm not sure why but the error that Unity gives is:
InvalidCasatException: Cannot cast from source type to destination type.
I'm not sure what this means.
Thanks for your help.
Answer by Eric5h5 · Jan 08, 2011 at 04:43 AM
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.<Vector3>()
, 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.
Answer by Jessy · Jan 08, 2011 at 03:40 AM
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;
Your answer
Follow this Question
Related Questions
transform.active question - Model Mesh Scripting 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Objects coming in front of other objects? 0 Answers
How would I implement this game mechanic? 0 Answers
Setting a mesh's triangles and vertices from file causes distortion. 2 Answers