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 MasterLetch · Dec 27, 2012 at 09:26 AM · meshprocedural

creating a mesh from script

i have no idea why this code isn't working... its almost exactly what is placed in the scripting reference

 using UnityEngine;
 using System.Collections;
 
 public class Window2 : MonoBehaviour {
     
     
       public Vector3[] Vertices;
     public Vector2[] UV;
     public int[] Triangles;
     
     void MeshSetup(){
      Vector3[] Vertices = new Vector3[] {new Vector3(-1,0,1),new Vector3(1,0,1),new Vector3(1,0,-1),new Vector3(-1,0,-1)};
      Vector2[] UV = new Vector2[] {new Vector2(0,256),new Vector2(256,256),new Vector2(256,0),new Vector2(0,0)};
      int[] Triangles = new int[] {0,1,2,0,2,3};
         
     }
     
     
     void Start() {
         MeshSetup();
         Mesh stuff = new Mesh();
         gameObject.AddComponent<MeshFilter>().mesh = stuff;
         stuff.vertices = Vertices;
         stuff.triangles = Triangles;
         stuff.uv = UV;
         
     }
 }

so thats what i have, judging by everything i have read, that should work. right now i get 0 verts , 0 tris on the mesh that is added...

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

Answer by Fattie · Dec 27, 2012 at 10:54 AM

Dude you're made a trivial mistake

(purely at the language level, unrelated to mesh technology)

In lines 12 through 15 you're delcaring NEW local variables inside the routine MeshSetup !

wrong ...

 Vector3[] Vertices = ...
 Vector2[] UV = ...
 int[] Triangles = ...

correct...

 Vertices = ...
 UV = ...
 Triangles = ...

Hope it helps!

PS1: a good trick is, ALSO just add a mesh collider with the same mesh, very simply so you can seee it in the editor, evene if you have not yet added an actual texture to the visual mesh. So simply add the line

 gameObject.AddComponent<MeshCollider>().mesh = stuff;

as the final line in Start(). TBC, note that of course you would not add collider mesh, at runtime in a game, only at startup time. Info here: http://answers.unity3d.com/questions/321428/adding-mesh-collider-in-run-time-slow.html

PS2: you will eventually need some normals. Creating normals is an art form, it is not something that can be done by the same software every time. Unity gives you for free a "rough and ready" normals routine which can be handy during development. it is called RecalculateNormals(). Understand that later you will have to write your own subtle normals-making software. Here is a discussion about that issue http://answers.unity3d.com/questions/329116/do-we-need-to-call-recalculatenormals-if-normal-ve.html

Comment
Add comment · Show 7 · 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 pako · Dec 27, 2012 at 11:06 AM 0
Share

Hi Fattie,

Yes I agree, just as I said in my answer above :-)

avatar image MasterLetch · Dec 27, 2012 at 07:27 PM 0
Share

That worked like a charm, thanks for the help. its sad but that had me for a couple days

avatar image iminimin MasterLetch · May 28, 2021 at 11:56 AM 0
Share

Hey, I follow the corrected code and attach it to a Empty_Game_Object, and also add the Mesh_Filter component to Empty_Game_Object, then hit the play button, but then nothing shows up. So, is it OK that you show your code which is working again?

I am also wandering should I follow link text of Unity Reference/Documentation to build a mesh I want. Because somehow they don't explain well, or maybe it's just me don't quite understand it.

avatar image Bunny83 · Dec 27, 2012 at 08:23 PM 1
Share

Uhm C# mistake? You can do this wrong in any language :)

avatar image Fattie · Dec 27, 2012 at 08:38 PM 0
Share

LOL for sure. I just meant "your problem here, was at the language level." ie, it has nothing to do with mesh, topology or what have you.

Essentially, we could perhaps call it a syntax error - although it's a little "higher level" than that. I don't know the best term for that exact type of error.

(Typical of the sad state of software engineering that all us guys don't have very specific words for these types of problems. All your buddies who are concrete engineers have incredibly specific terms for "we didn't put in enough water" "the pump got clogged" etc !)

avatar image Fattie · Dec 27, 2012 at 08:39 PM 0
Share

Now I'll lose sleep for three days thinking of the best way to describe that particular type of program$$anonymous$$g error.

avatar image Topillogical Fattie · Jul 21, 2016 at 02:06 PM 1
Share

The program$$anonymous$$g error is called variable shadowing.

avatar image
3

Answer by pako · Dec 27, 2012 at 10:49 AM

You are re-declaring the type of Vertices, UV, Triangle variables inside of the MeshSetup() method. As you have it now, they are treated as local variables, and their values are not assigned to the publicly declared variables of the same name, so, whatever is assigned to those values when the method runs are immediately forgoten when the method finishes.

This should work:

 using UnityEngine;
 using System.Collections;
 
 public class Window2 : MonoBehaviour {
 
 
     public Vector3[] Vertices;
     public Vector2[] UV;
     public int[] Triangles;
 
     void MeshSetup(){
      Vertices = new Vector3[] {new Vector3(-1,0,1),new Vector3(1,0,1),new Vector3(1,0,-1),new Vector3(-1,0,-1)};
      UV = new Vector2[] {new Vector2(0,256),new Vector2(256,256),new Vector2(256,0),new Vector2(0,0)};
      Triangles = new int[] {0,1,2,0,2,3};
 
     }
 
 
     void Start() {
        MeshSetup();
         Mesh stuff = new Mesh();
        gameObject.AddComponent<MeshFilter>().mesh = stuff;
        stuff.vertices = Vertices;
        stuff.triangles = Triangles;
         stuff.uv = UV;
 
     }
 }
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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generate mesh from raycast positions, independent of rotations 0 Answers

What's the Script for Combining Prefabs 2 Answers

Create the visual spring in Unity? 3 Answers

Create planar UVs on procedurally generated mesh 1 Answer

What is wrong with this mesh editing code? 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