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 kag359six · Aug 26, 2012 at 03:58 PM · instantiateforeachloops

Why aren't the right amount of blocks instantiating?

In the followings script assume everything is defined properly. The BlockData.currentChunk variable holds the chunk positions. All chunk objects are instantiated at the start. (chunk set up is 6x1x6). I check if the distance between the player and the chunk is less than 20, and if it is I add the chunk to a list of Transforms named chunksToRender. Then i go into my renderBlocks function and cycle through each chunk in the list. It should only render blocks in the chunks in that list, which the debug.Log shows as 4 chunks. Therefore only 40 blocks (each chunk is 10x1x10), however it still renders all 400 of them. If anyone could help out I'd GREATLY appreciate it as this is something I REALLY want to figure out.

ALSO THE MAIN FOCUS IS THE CREATECHUNKS() FUNCTION AND RENDERBLOCKS() FUNCTION

 using System.Collections.Generic;
 
 public class terrain : MonoBehaviour {
     
     public int blocksX = 20;
     public int blocksY = 10;
     public int blocksZ = 20;
     
     public Transform block;
     public Transform chunkPrefab;
     
     public List<Transform> chunksToRender = new List<Transform>();
     
     public Transform player;
     
     public bool renderBlocks = false;
     
     void Start () {
     
         createChunks();
 
         generateTerrainMaterials();
         
     }
     
     void Update() {
         
         if(renderBlocks) {
             
             renderBlock();
             
         }
         
     }
     
     
     private void generateTerrainMaterials() {
         
         foreach(Transform obj in BlockData.blockGO) { //cycle through each block 
             
             if(obj.transform.position.y >= 3) //assign grass material if block is a surface block
             
                 obj.renderer.material = (Material)Resources.Load ("Materials/grass");
                 
             }
         
     }
     
     private void renderBlock() {
         int blocksRndered = 0;
         BlockData.blockGO = new Transform[blocksX,blocksY,blocksZ];
         foreach(Transform curChunk in chunksToRender) {
             
             float curChunkX = curChunk.position.x;
             float curChunkZ = curChunk.position.z;
             
             for(int x = 0; x < blocksX; x++) {
                 
                 for(int y = 0; y < blocksY; y++) {
                                     
                     for(int z = 0; z < blocksZ; z++) {
 
                         Vector3 blockPos = new Vector3(x * curChunkX, y, z * curChunkZ);
                         Transform blockPrefab = Instantiate(block, blockPos, Quaternion.identity) as Transform;
                         blockPrefab.renderer.material = (Material)Resources.Load("Materials/soil");
                         BlockData.blockGO[x,y,z] = blockPrefab;
                         blocksRndered++;                
                     }
                                     
                 }
                                 
             }
         
         }
         
         Debug.Log ("blocks rendered: " + blocksRndered);
         renderBlocks = false;
         
     }
     
     private void createChunks() {
         
         BlockData.currentChunk = new Transform[BlockData.chunkX, BlockData.chunkY, BlockData.chunkZ];
         
         int numberOfChunks = 0;
         
         for(int cx = 0; cx < BlockData.chunkX; cx++) {
             
             for(int cy = 0; cy < BlockData.chunkY; cy++) {
                 
                 for(int cz = 0; cz < BlockData.chunkZ; cz++) {
                     
                     Vector3 chunkPos = new Vector3(blocksX * cx, blocksY * cy, blocksZ * cz);
                     Transform chunkInst = Instantiate(chunkPrefab,chunkPos, Quaternion.identity) as Transform;
                     BlockData.currentChunk[cx,cy,cz] = chunkInst;
                     if(Vector3.Distance(player.position,chunkInst.position) < 20) {
                         
                         chunksToRender.Add(chunkInst);    
                         
                     }
                     numberOfChunks += 1;
                 }
                 
             }
             
         }
         renderBlocks = true;
         Debug.Log("number of chunks: " + numberOfChunks + " chunks to render: " + chunksToRender.Count);
     
     }
 }
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 kag359six · Aug 26, 2012 at 04:01 PM 0
Share

ALso the Debug.Log at the bottom of the script displays the correct number of chunks, and number of chunks to render, but it still renders more that amount.

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

7 People are following this question.

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

Related Questions

Foreach loop not going through all the children 1 Answer

Having a strange problem with yield and instantiate (C#) 1 Answer

For Loop isn't working properly! 1 Answer

Instantiate inside foreach loop 1 Answer

Checking if object intersects? 1 Answer


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