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 velv · Jun 15, 2012 at 08:28 PM · meshprocedural

Creating a Pentahedron (a 5 sided shape) Mesh procedurally

I try to create a 5 sided shape mesh procedurally in a JS script, then i dragged and dropped it in the Main Camera so i can see it, here's the code:

 function Start() {
 
     var tmpobj: GameObject = new GameObject();
     
     tmpobj.AddComponent(MeshFilter);
     tmpobj.AddComponent(MeshRenderer);
     
     var verts: Vector3[] = new Vector3[5];
     var normals: Vector3[] = new Vector3[5];
     var uv: Vector2[] = new Vector2[5];
     var tri: int[] = new int[12]; //3 vertices * 4 triangles =12
    
     var mf: MeshFilter = GetComponent(MeshFilter);
    
    //vertices positioning of the points P1,P2,P3,P4,P5
     verts[0] = new Vector3(0, 0, 0); //P5
     verts[1] = new Vector3(-5, 0, -5); //P1
     verts[2] = new Vector3(-5, 0, 5); //P2
     verts[3] = new Vector3(5, 0, 5); //P3
     verts[4] = new Vector3(5, 0, -5); //P4
    
     for (i = 0; i < normals.Length; i++) {
         normals[i] = Vector3.up;
     }
    
     uv[0] = new Vector2(0, 0);
     uv[1] = new Vector2(1, 0);
     uv[2] = new Vector2(0, 1);
     uv[3] = new Vector2(1, 1);
     uv[4] = new Vector2(0, 0);
     
     //triangles (clockwise)
     tri[0] = 0;
     tri[1] = 1;
     tri[2] = 2;
    
     tri[3] = 0;
     tri[4] = 2;
     tri[5] = 3;
     
     tri[6] = 0;
     tri[7] = 3;
     tri[8] = 4;
    
     tri[9] = 0;
     tri[10] = 4;
     tri[11] = 1;
     
     var mesh: Mesh = new Mesh();
     mesh.name = "Roof_01";
     mesh.vertices = verts;
     mesh.triangles = tri;
     mesh.uv = uv;
     mesh.normals = normals;
     mesh.RecalculateNormals();
     mf.mesh = mesh;
     
     
 }

I don't know what i'm doing wrong and i can't see the mesh. The thing is it's my first time using Meshes by code and i'm not familiar with them yet. I get that exception:

"MissingComponentException: There is no 'MeshFilter' attached to the "Main Camera" game object, but a script is trying to access it. You probably need to add a MeshFilter to the game object "Main Camera". Or your script needs to check if the component is attached before using it. Roof.Start () (at Assets/Roof.js:79)"

Any idea why i can't see the Mesh? Thanks in advance.

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
0
Best Answer

Answer by Wolfram · Jun 15, 2012 at 08:32 PM

Replace GetComponent(MeshFilter) with tmpobj.GetComponent(MeshFilter), you're trying to find the MeshFilter on your current object.

Comment
Add comment · Show 2 · 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 Wolfram · Jun 15, 2012 at 08:33 PM 1
Share

Or, slightly more efficient, remove that line altogether, and replace the line at the beginning of your script with var mf: $$anonymous$$eshFilter = tmpobj.AddComponent($$anonymous$$eshFilter);

avatar image velv · Jun 16, 2012 at 04:07 PM 0
Share

Excellent @Wolfram !! Thank you. Now i can see my mesh.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Determine whether or not model is "inside out" 2 Answers

Combining meshes (different materials) together for rotation/translation 1 Answer

Custom Mesh UV Problem 1 Answer

Mesh return vs out Mesh 2 Answers

Strange shading on procedural mesh 0 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