• 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
1
Question by Joshua · May 05, 2011 at 03:04 AM · mesharrayproceduralgenerationbce0044

Procedurally generated mesh JS - explain the error please

Hey guys, I decided to try my luck at procedurally generating meshes. I've tried following this guide, but since it's in C# I wanted to translate it into JS. Since the general idea translates I thought I was managing quite well, but there are some compiler errors I keep getting that I cannot explain.

It's probably something extremely simple and stupid I'm doing wrong/overlooking and might very well have to do with me not understanding built in arrays very well (finding unity related documentation on them is proving difficult)

TBH I'm just a noob when it comes to scripting who helps out the even lesser noobs around here ^.^ Also, it's late, I'm tired and I need heeeelp :p I've added text explaining the code and pointing out the errors.

@MenuItem ("GameObject/Create Other/Tetrahedron") //add it to the menu

static function CreateTetrahedron () {

 var Tetrahedron : GameObject = new GameObject ("Tetrahedron");  //create an empty gameobject with that name

 Tetrahedron.AddComponent(MeshFilter);                           //add a meshfilter

 var p0 : Vector3 = Vector3(0,0,0);                              //the four points that make a tetrahedron
 var p1 : Vector3 = Vector3(1,0,0);
 var p2 : Vector3 = Vector3(0.5f,0,Mathf.Sqrt(0.75f));
 var p3 : Vector3 = Vector3(0.5f,Mathf.Sqrt(0.75f),Mathf.Sqrt(0.75f)/3);

 var newVertices : Vector3[] = new Vector3[] {                   //the non-shared vertices (line 14)
     p0,p1,p2,p0,                                                //Assets/Editor/Tetrahedron.js(14,50): BCE0044: expecting (, found '['.
     p2,p3,p2,p1,                                                //Assets/Editor/Tetrahedron.js(15,19): BCE0044: expecting :, found ','.
     p3,p0,p3,p1
 };

 var newTriangles : int[] = new int[]{                           //the triangle faces the points make up
     0,1,2,
     0,2,3,
     2,1,3,
     0,3,1
 };

 var newMesh : Mesh = new Mesh ();                               //create a new mesh, assign the vertices and triangles
     newMesh.vertices = newVertices;
     newMesh.triangles = newTriangles;
     newMesh.RecalculateNormals();                               //recalculate normals, bounds and optimize
     newMesh.RecalculateBounds();
     newMesh.Optimize();                             

 Tetrahedron.GetComponent(MeshFilter).mesh = newMesh;            //assign the created mesh as the used mesh

}

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

1 Reply

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

Answer by loramaru · May 05, 2011 at 03:42 AM

The problem is that you're using the C# array initialization. Here's what you want for JavaScript.

var newVertices : Vector3[] = [ p0,p1,p2,p0, p2,p3,p2,p1, p3,p0,p3,p1 ];

var newTriangles : int[] = [
0,1,2, 0,2,3, 2,1,3, 0,3,1 ];

Also you had one more error that crops up once those are fixed; so here's a bonus fix. :)

(Tetrahedron.GetComponent(MeshFilter) as MeshFilter).mesh = newMesh;
Comment
Add comment · Show 5 · 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 Joshua · May 05, 2011 at 03:44 AM 0
Share

Haha , proves how blind you become when you stare too long at something. Thanks a lot :-)

avatar image loramaru · May 05, 2011 at 03:50 AM 0
Share

Also you mixed up the vertices from the second example with the triangles from the first. Your code will generate a flat triangle. You really want... var newTriangles : int[] = [0,1,2,3,4,5,6,7,8,9,10,11];

avatar image Joshua · May 05, 2011 at 03:56 AM 0
Share

Yes! Thank you! I got it to work :D thank you very very very very much!

avatar image keenanwoodall · Jul 15, 2014 at 04:37 PM 0
Share

How would I make the mesh visible, i couldn't figure it out

avatar image keenanwoodall · Jul 15, 2014 at 05:28 PM 0
Share

How would I make the pyramid visible? I've tried adding a renderer but I couldn't get it to work

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Procedural Mesh Generation - Split Arrays into sections 1 Answer

creating a mesh procedurally 4 Answers

Most efficient method to create mesh from array 1 Answer

Procedurally Generated Cube Mesh 3 Answers

Unity3D question about the Procedural Generation engine for creatures in Spore? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges