Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thomas 3 · Jan 08, 2011 at 03:09 AM · meshobjectmodel

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thomas 3 · Jan 08, 2011 at 04:08 PM 0
Share

Thanks for the help, its working now.

avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges