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 Macuser47 · Oct 27, 2014 at 02:45 AM · c#debug

Creation of unnecessary and unwanted cubes

The intended outcome of this script is to generate a series of cubes in one chunk, with the cubes not exposed to the outside not generated at all, but for some reason that I cannot seem to find, the cubes not exposed to the outside are generating, causing massive lag...

Note: This script is attached to a first person controller

Note: I know the color scripts are not properly implemented, but that isn't the point of this question.

 using UnityEngine;
 using System.Collections;
 
 public class GenerateChunck : MonoBehaviour {
 
     // constatnts
     public const int BLOCKSIZE = 32;
     public const int BLOCKSOLIDSIZE = 29;
     public const int CHUNCKSIZE = 32;
 
     public const int BLOCKTYPEAIR = 0;
     public const int BLOCKTYPEDIRT = 1;
     public const int BLOCKTYPESTONE = 2;
     public const int BLOCKTYPEOBSIDIAN = 3;
     public const int BLOCKTYPEGRASS = 4;
     public const int BLOCKTYPEBEDROCK = 5;
     public const int BLOCKTYPEWOOD = 6;
     public const int BLOCKTYPELEAVES = 7; 
 
 
 
     //variables and arrays
     int[,,] chunkArray;
     int cubeCount;
 
     void Start () {
         cubeCount = 0;
         initArray ();
         clearArray ();
         populateArray ();
         drawArray ();
         print (cubeCount);
     }
 
     private void initArray()
     {
         chunkArray = new int[CHUNCKSIZE, CHUNCKSIZE, CHUNCKSIZE];
     }
 
     private void clearArray()
     {
         for (int arrayX = 0; arrayX < CHUNCKSIZE; arrayX ++)
         {
             for (int arrayY = 0; arrayY < CHUNCKSIZE; arrayY ++)
             {
                 for (int arrayZ = 0; arrayZ < CHUNCKSIZE; arrayZ ++)
                 {
                     setArrayBlock(arrayX, arrayY, arrayZ, BLOCKTYPEAIR);
                 }
             }
         }
     }
 
     private void setArrayBlock(int blockX, int blockY, int blockZ, int blockType)
     {
         chunkArray[blockX, blockY, blockZ] = blockType;
     }
 
     private void populateArray()
     {
         setArrayLayer(0, BLOCKTYPEBEDROCK);
         setArrayLayer(1, BLOCKTYPEAIR);
         setArrayLayer(2, BLOCKTYPEAIR);
         setArrayLayer(3, BLOCKTYPEAIR);
         setArrayLayer(4, BLOCKTYPEAIR);
         setArrayLayer(5, BLOCKTYPEAIR);
         setArrayLayer(6, BLOCKTYPEAIR);
         setArrayLayer(7, BLOCKTYPEAIR);
         setArrayLayer(8, BLOCKTYPEAIR);
         setArrayLayer(9, BLOCKTYPEAIR);
         setArrayLayer(10, BLOCKTYPEAIR);
         setArrayLayer(11, BLOCKTYPEAIR);
         setArrayLayer(12, BLOCKTYPEAIR);
         setArrayLayer(13, BLOCKTYPEAIR);
         setArrayLayer(14, BLOCKTYPEAIR);
         setArrayLayer(15, BLOCKTYPEAIR);
         setArrayLayer(16, BLOCKTYPEGRASS);
         setArrayLayer(17, BLOCKTYPEDIRT);
         setArrayLayer(18, BLOCKTYPEDIRT);
         setArrayLayer(19, BLOCKTYPEDIRT);
         setArrayLayer(20, BLOCKTYPESTONE);
         setArrayLayer(21, BLOCKTYPESTONE);
         setArrayLayer(22, BLOCKTYPESTONE);
         setArrayLayer(23, BLOCKTYPESTONE);
         setArrayLayer(24, BLOCKTYPESTONE);
         setArrayLayer(25, BLOCKTYPESTONE);
         setArrayLayer(26, BLOCKTYPESTONE);
         setArrayLayer(27, BLOCKTYPESTONE);
         setArrayLayer(28, BLOCKTYPESTONE);
         setArrayLayer(29, BLOCKTYPESTONE);
         setArrayLayer(30, BLOCKTYPEOBSIDIAN);
         setArrayLayer(31, BLOCKTYPEBEDROCK);
         setArrayBlock(4, 15, 4, BLOCKTYPEDIRT);
     }
 
     private void setArrayLayer(int layerY, int blockType)
     {
         for (int arrayX = 0; arrayX < CHUNCKSIZE; arrayX ++)
         {
             for (int arrayZ = 0; arrayZ < CHUNCKSIZE; arrayZ ++)
             {
                 setArrayBlock(arrayX, layerY, arrayZ, blockType);
             }
         }
     }
 
     private void drawArray()
     {
         for (int arrayX = 0; arrayX < CHUNCKSIZE; arrayX ++)
         {
             for (int arrayY = 0; arrayY < CHUNCKSIZE; arrayY ++)
             {
                 for (int arrayZ = 0; arrayZ < CHUNCKSIZE; arrayZ ++)
                 {
                     if (chunkArray[arrayX, arrayY, arrayZ] != BLOCKTYPEAIR)
                     {
                         //getFill(chunkArray[arrayX][arrayY][arrayZ]);
                         //updateCollision(arrayX, arrayY, arrayZ);
                         if (checkSurroundings(arrayX, arrayY, arrayZ))
                         {
                             drawCubeWithColor(arrayX * BLOCKSIZE, arrayY * BLOCKSIZE, arrayZ * BLOCKSIZE, BLOCKSOLIDSIZE, getFill(chunkArray[arrayX, arrayY, arrayZ]));
                         }
                     }
                 }
             }
         }
     }
 
     private bool checkSurroundings(int arrayX, int arrayY, int arrayZ)
     { 
         if (arrayX == 0 || arrayX == 31 || arrayY == 0 || arrayY == 31 || arrayZ == 0 || arrayZ == 31)
             return true;
         else if (checkArrayEmpty(arrayX + 1, arrayY, arrayZ) || checkArrayEmpty(arrayX - 1, arrayY, arrayZ))
             return true;
         else if (checkArrayEmpty(arrayX, arrayY + 1, arrayZ) || checkArrayEmpty(arrayX, arrayY - 1, arrayZ))
             return true; 
         else if (checkArrayEmpty(arrayX, arrayY, arrayZ + 1) || checkArrayEmpty(arrayX, arrayY, arrayZ - 1))
             return true;
         else
             return false;
     }
 
 
     private bool checkArrayEmpty(int arrayX, int arrayY, int arrayZ)
     {
         return (getArrayBlock(arrayX, arrayY, arrayZ) == 0);
     }
 
     private void drawCubeWithColor(int xPosition, int yPosition, int zPosition, int blockSize, Color color)
     {
         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.AddComponent<Rigidbody>();
         cube.transform.position = new Vector3(xPosition, yPosition, zPosition);
         cube.transform.localScale = new Vector3(blockSize, blockSize, blockSize);
         cube.renderer.material.color = color;
         cubeCount++;
     }
 
     private Color getFill(int blockType)
     {
         if (blockType == BLOCKTYPEDIRT)
             return new Color(51, 25, 0);
         else if (blockType == BLOCKTYPEGRASS)
             return new Color(10, 250, 15);
         else if (blockType == BLOCKTYPESTONE)
             return new Color(125, 125, 125);
         else if (blockType == BLOCKTYPEOBSIDIAN) 
             return new Color(13, 0, 25);
         else if (blockType == BLOCKTYPEBEDROCK) 
             return new Color(10, 10, 10);
         else if (blockType == BLOCKTYPEWOOD) 
             return new Color(102, 51, 0);
         else if (blockType == BLOCKTYPELEAVES)
             return new Color(51, 102, 0); 
         else
             return new Color(0, 0, 0);
     }
 
     private int getArrayBlock(int blockX, int blockY, int blockZ)
     {
         
         if (blockX < 0 || blockX > 31 || blockY < 0 || blockY > 31 || blockZ < 0 || blockZ > 31)  
             return BLOCKTYPEAIR;
         else
             return chunkArray[blockX, blockY, blockZ];
     }
 
 
 }


Comment
Add comment · Show 1
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 richyrich · Oct 27, 2014 at 09:58 AM 0
Share

Why add a rigidbody to every cube? That's quite an overhead (32x32x32 = 32728) - $$anonymous$$us half for air, still gives 15k Rigibodies. Surely a better approach is to just add a rigidbody when it is needed, maybe ins$$anonymous$$d just to those objects that will interact with the cubes?

As for your question, I can't see anything that pops out, however I would suggest using a $$anonymous$$UCH smaller dataset and then Debug.Log your functions to see if the execution path is doing everything you think it should in the way you intended

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What port does mono's debugger use 1 Answer

Generic Debug Message 1 Answer

Question on Strange Debug.Log() Behavior 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