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
1
Question by Monko · Apr 12, 2013 at 12:58 AM · errormeshprocedural

Unity isn't reading the mesh I created

In my game, I am proceduraly drawing a mesh. I know how to draw the mesh, so now Im putting it into a function so I can call the function later. Here is my code so far:

 function Start(){
 
 var block : GameObject = new GameObject("Block");
 var mesh : Mesh = new Mesh ();
 var meshFilter = block.AddComponent(MeshFilter); 
 block.AddComponent(MeshRenderer); 
 block.renderer.material = Resources.Load("VoxelMaterial");
 DrawTheMesh();
 }
 
 function DrawTheMesh() {
 
 var vertices = new Vector3[4]; 
 var triangles = new int[6];
 vertices[0] = Vector3(0,0,0); 
 vertices[1] = Vector3(0,0,1); 
 vertices[2] = Vector3(1,0,1);
 vertices[3] = Vector3(1,0,0);
 triangles = [0,1,2,0,2,3];
 mesh.vertices = vertices; 
 mesh.triangles = triangles; 
 var uvs = new Vector2[mesh.vertices.length];
 for (var i=0; i < uvs.Length; i+=1) {
 uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
 }
 
 }

I know this code works just fine, because Ive drawn a shape with it before. But when I put it into a function called DrawTheMesh, it gives an error saying "unknown identifier: "mesh" ". Which doesn't make sense, since I clearly defined it at the very beggining during the Start function. Does anyone know why its doing this?

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

Answer by robertbu · Apr 12, 2013 at 01:02 AM

You are declaring 'mesh' inside Start() which means that mesh exists only during the lifetime of that specific Start() call, and only in the scope of the Start() function. Move the declaration to the top of the file (above the start function). You should be able to initialize it there as well.

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 Monko · Apr 12, 2013 at 01:33 AM 0
Share

That explains so much!! Thank you for saying that. Anyways, I now understand that if im going to need a variable to operate in many different functions, i need to declare them out of the start function to do so. Sadly, doing this caused more errors.

The errors say this:

"you are not allowed to call this function when declaring a variable"

"GameObject can only be called from the main thread"

I don't understand what the first error message means, and what is the main thread? how do I solve it?

avatar image robertbu · Apr 12, 2013 at 02:06 AM 0
Share

It means that you need to execute the code inside Start() or some other function. So for your 'block' variable, you will put this at the top of the file:

 var block : GameObject;

Then inside of Start() you will put:

 block =  new GameObject("Block");
avatar image Monko · Apr 12, 2013 at 02:36 AM 0
Share

Wow, can I just say I have learned a lot from your posts robertbu. I didn't know how to initiate the variable first, and then declare its value inside start() until now. And it all makes sense! I have only one error left, because all the other 6 error messages become solved.

For some reason, its sending an error that sais "Null reference exception". It sends me to line 28 of the code below:

     internal var block : GameObject;
     internal var mesh : $$anonymous$$esh;
     internal var meshFilter;
     
     
     
     function Start(){
     
     
     DrawThe$$anonymous$$esh();
     block =  new GameObject("Block");
     mesh = new $$anonymous$$esh ();
     meshFilter = block.AddComponent($$anonymous$$eshFilter);
     block.AddComponent($$anonymous$$eshRenderer); 
     block.renderer.material = Resources.Load("Voxel$$anonymous$$aterial");
     
     }
     
     function DrawThe$$anonymous$$esh() {
     
     var vertices = new Vector3[4]; 
     var triangles = new int[6];
     vertices[0] = Vector3(0,0,0); 
     vertices[1] = Vector3(0,0,1); 
     vertices[2] = Vector3(1,0,1);
     vertices[3] = Vector3(1,0,0);
     triangles = [0,1,2,0,2,3];
     mesh.vertices = vertices; //line 28
     mesh.triangles = triangles; 
     var uvs = new Vector2[mesh.vertices.length];
     for (var i=0; i < uvs.Length; i+=1) {
     uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
     }
     
     }


I believe it thinks that object "mesh" that i made is a null reference, which is weird considering I assigned it. Any thoughts?

avatar image whebert · Apr 12, 2013 at 03:07 AM 1
Share

Because you are calling DrawThe$$anonymous$$esh(); in your Start function before you instantiate your mesh, so there is no mesh yet when you're trying to assign vertices to it. You need to call DrawThe$$anonymous$$esh(); after your mesh setup.

Also, you need to assign your mesh to your meshFilter as well.

 meshFilter.mesh = mesh;

And might want to assign your uvs to your mesh at the end...

 mesh.uv = uvs;
avatar image Monko · Apr 12, 2013 at 03:18 AM 0
Share

Thankyou very much. The errors are finaly solved.

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

11 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

Related Questions

Landscape has a awry vertex 1 Answer

Why isn't this mesh not appearing properly? 0 Answers

creating a mesh procedurally 4 Answers

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 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