Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by nitrogames79 · Jul 12, 2020 at 07:54 AM · classnullclass instancereturn value

Returned class is always null

The MeshHolder class that GenerateChunkMesh returns is always null. No matter what I do to try and make it return the class it continues to return null.

 public class MeshGenerator {
         private byte[,,] faces = new byte[Chunk.chunksize, Chunk.chunksize, Chunk.chunksize];
     
         private Voxel[,,] blocks = new Voxel[Chunk.chunksize, Chunk.chunksize, Chunk.chunksize];
     
         private Vector3[] vertices;
         private int[] triangles;
         private Vector2[] uvs;
     
         private int verticesPrediction;
     
         private int verticesIndex, trianglesIndex;
     
         private Vector3 pos;
         private World world;
     
         private Voxel newblock;
     
         public MeshGenerator(Vector3 pos, Voxel[,,] blocks, World world) {
             this.pos = pos;
             this.blocks = blocks;
             this.world = world;
         }
     
         public MeshHolder GenerateChunkMesh() {
             Voxel newblock;
             for (int x = 0; x < Chunk.chunksize; x++){
                 for (int y = 0; y < Chunk.chunksize; y++){
                     for (int z = 0; z < Chunk.chunksize; z++){
                         if(blocks[x, y, z].IsTransparent()){
                             continue;
                         }
                         if (MathVox.InsideChunk(z + 1)){
                             if (blocks[x, y, z + 1].IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.north;
                                 verticesPrediction += 4;
                             }
                         }
                         else{
                             newblock = world.GetBlock(pos, x, y, z + 1);
                             //if (newblock.IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.north;
                                 verticesPrediction += 4;
                             //}
                         }
                         if (MathVox.InsideChunk(z - 1)){
                             if (blocks[x, y, z - 1].IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.south;
                                 verticesPrediction += 4;
                             }
                         }
                         else{
                             newblock = world.GetBlock(pos, x, y, z - 1);
                             if (newblock.IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.south;
                                 verticesPrediction += 4;
                             }
                         }
                         if (MathVox.InsideChunk(y + 1)){
                             if (blocks[x, y + 1, z].IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.up;
                                 verticesPrediction += 4;
                             }
                         }
                         else{
                             newblock = world.GetBlock(pos, x, y + 1, z);
                             if (newblock.IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.up;
                                 verticesPrediction += 4;
                             }
                         }
                         if (MathVox.InsideChunk(y - 1)){
                             if (blocks[x, y - 1, z].IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.down;
                                 verticesPrediction += 4;
                             }
                         }
                         else{
                             newblock = world.GetBlock(pos, x, y - 1, z);
                             if (newblock.IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.down;
                                 verticesPrediction += 4;
                             }
                         }
                         if (MathVox.InsideChunk(x + 1)){
                             if (blocks[x + 1, y, z].IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.east;
                                 verticesPrediction += 4;
                             }
                         }
                         else{
                             newblock = world.GetBlock(pos, x + 1, y, z);
                             if (newblock.IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.east;
                                 verticesPrediction += 4;
                             }
                         }
                         if (MathVox.InsideChunk(x - 1)){
                             if (blocks[x - 1, y, z].IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.west;
                                 verticesPrediction += 4;
                             }
                         }
                         else{
                             newblock = world.GetBlock(pos, x - 1, y, z);
                             if (newblock.IsTransparent()){
                                 faces[x, y, z] |= (byte)Direction.west;
                                 verticesPrediction += 4;
                             }
                         }
                     }
                 }
             }
     
             if (verticesPrediction == 0){
                 Debug.Log("pass");
                 //return null;
             }
     
             vertices = new Vector3[verticesPrediction];
             uvs = new Vector2[verticesPrediction];
             triangles = new int[(int)(verticesPrediction * 1.5f)];
     
             for (int x = 0; x < Chunk.chunksize; x++){
                 for (int y = 0; y < Chunk.chunksize; y++){
                     for (int z = 0; z < Chunk.chunksize; z++){
                         if (faces[x, y, z] == 0){
                             continue;
                         }
                         if ((faces[x, y, z] & (byte)Direction.north) != 0){
                             vertices[verticesIndex] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 2] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
     
                             triangles[trianglesIndex] = verticesIndex + 2;
                             triangles[trianglesIndex + 1] = verticesIndex + 1;
                             triangles[trianglesIndex + 2] = verticesIndex;
     
                             triangles[trianglesIndex + 3] = verticesIndex;
                             triangles[trianglesIndex + 4] = verticesIndex + 3;
                             triangles[trianglesIndex + 5] = verticesIndex + 2;
     
                             TextureManager.AddTextures(blocks[x, y, z], Direction.north, verticesIndex, uvs);
     
                             verticesIndex += 4;
                             trianglesIndex += 6;
                         }
                         if ((faces[x, y, z] & (byte)Direction.south) != 0){
                             vertices[verticesIndex] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 2] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
     
                             triangles[trianglesIndex] = verticesIndex;
                             triangles[trianglesIndex + 1] = verticesIndex + 1;
                             triangles[trianglesIndex + 2] = verticesIndex + 2;
     
                             triangles[trianglesIndex + 3] = verticesIndex + 2;
                             triangles[trianglesIndex + 4] = verticesIndex + 3;
                             triangles[trianglesIndex + 5] = verticesIndex;
     
                             TextureManager.AddTextures(blocks[x, y, z], Direction.south, verticesIndex, uvs);
     
                             verticesIndex += 4;
                             trianglesIndex += 6;
                         }
                         if ((faces[x, y, z] & (byte)Direction.up) != 0){
                             vertices[verticesIndex] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 2] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
     
                             triangles[trianglesIndex] = verticesIndex;
                             triangles[trianglesIndex + 1] = verticesIndex + 1;
                             triangles[trianglesIndex + 2] = verticesIndex + 2;
     
                             triangles[trianglesIndex + 3] = verticesIndex + 2;
                             triangles[trianglesIndex + 4] = verticesIndex + 3;
                             triangles[trianglesIndex + 5] = verticesIndex;
     
                             TextureManager.AddTextures(blocks[x, y, z], Direction.up, verticesIndex, uvs);
     
                             verticesIndex += 4;
                             trianglesIndex += 6;
                         }
                         if ((faces[x, y, z] & (byte)Direction.down) != 0){
                             vertices[verticesIndex] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 2] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
     
                             triangles[trianglesIndex] = verticesIndex + 2;
                             triangles[trianglesIndex + 1] = verticesIndex + 1;
                             triangles[trianglesIndex + 2] = verticesIndex;
     
                             triangles[trianglesIndex + 3] = verticesIndex;
                             triangles[trianglesIndex + 4] = verticesIndex + 3;
                             triangles[trianglesIndex + 5] = verticesIndex + 2;
     
                             TextureManager.AddTextures(blocks[x, y, z], Direction.down, verticesIndex, uvs);
     
                             verticesIndex += 4;
                             trianglesIndex += 6;
                         }
                         if ((faces[x, y, z] & (byte)Direction.east) != 0){
                             vertices[verticesIndex] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 1] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 2] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
     
                             triangles[trianglesIndex] = verticesIndex + 2;
                             triangles[trianglesIndex + 1] = verticesIndex + 1;
                             triangles[trianglesIndex + 2] = verticesIndex;
     
                             triangles[trianglesIndex + 3] = verticesIndex;
                             triangles[trianglesIndex + 4] = verticesIndex + 3;
                             triangles[trianglesIndex + 5] = verticesIndex + 2;
     
                             TextureManager.AddTextures(blocks[x, y, z], Direction.east, verticesIndex, uvs);
     
                             verticesIndex += 4;
                             trianglesIndex += 6;
                         }
                         if ((faces[x, y, z] & (byte)Direction.west) != 0){
                             vertices[verticesIndex] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
                             vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 2] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
                             vertices[verticesIndex + 3] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
     
                             triangles[trianglesIndex] = verticesIndex;
                             triangles[trianglesIndex + 1] = verticesIndex + 1;
                             triangles[trianglesIndex + 2] = verticesIndex + 2;
     
                             triangles[trianglesIndex + 3] = verticesIndex + 2;
                             triangles[trianglesIndex + 4] = verticesIndex + 3;
                             triangles[trianglesIndex + 5] = verticesIndex;
     
                             TextureManager.AddTextures(blocks[x, y, z], Direction.west, verticesIndex, uvs);
     
                             verticesIndex += 4;
                             trianglesIndex += 6;
                         }
                     }
                 }
             }
             return new MeshHolder(vertices, triangles, uvs);
         }
     }
     
         public class MeshHolder {
     
             private Vector3[] vertices;
             private int[] triangles;
             private Vector2[] uvs;
     
             public MeshHolder(Vector3[] vertices, int[] triangles, Vector2[] uvs){
                 this.vertices = vertices;
                 this.triangles = triangles;
                 this.uvs = uvs;
             }
     
             public Mesh CreateMesh(){
                 Mesh mesh = new Mesh();
     
                 if(vertices == null){
                     return null;
                 }
     
                 mesh.vertices = vertices;
                 mesh.triangles = triangles;
                 //mesh.uv = uvs;
     
                 mesh.RecalculateNormals();
     
                 return 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
0
Best Answer

Answer by nitrogames79 · Jul 23, 2020 at 08:17 PM

The code that got the MeshHolder was

 public void GetMesh(){
         MeshHolder myMesh = new MeshHolder();
         meshGen = new MeshGenerator(pos, blocks, world);
         myMesh = meshGen.GenerateChunkMesh();
 
         if(myMesh != null){
             mesh = myMesh.CreateMesh();
         }
         updateChunk = false;
     }

I got rid of MeshHolder myMesh = new MeshHolder(); and that ended up fixing it.

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

204 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

FindGameObjectsWithTag returns null objects 0 Answers

Way to copy a Class without copying the references? 0 Answers

ArgumentException: The prefab you want to instantiate is null. 2 Answers

Having issues with a NullReferenceException error. Code does as expected in some instances but I receive the error in others. Debugger directs me to line where if(hit.collider.tag == (DestructibleCube")). 1 Answer

Modify Custom Array Data 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