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 kag359six · Sep 05, 2012 at 09:32 PM · instantiateforeachfor loop

A way to destroy these blocks faster?

im trying to destroy the chunk of blocks as quickly as they are instantiated. Basically blocks are destroyed and instantiated depending on the player's distance from the chunk. When blocks are instantiated, they quickly instantiate as if the entire chunk in instantiating at once. However, when it comes to destroying them, it seems that they are destroyed in flowing rows, like this (may not be a good image):

alt text

So the questions is, how do i destroy the blocks as quickly as they are being instanced? Does it have to do with using a foreach loop and a list? Here's the code:

         Vector3 chunkCenter = new Vector3(transform.position.x + BlockData.chunkX / 2, BlockData.chunkY, transform.position.z + BlockData.chunkZ/2);
         
         if(Vector3.Distance(chunkCenter,player.transform.position) > terrain.maxViewDistance) {
             terrain.chunksToRender.Remove(transform);
             foreach(Transform block in terrain.blockList) { 
                 
                 if(block != null) {
                     if(block.position.x >= transform.position.x    && block.position.x < transform.position.x + terrain.blocksX) {
                         
                         if(block.position.z >= transform.position.z && block.position.z < transform.position.z + terrain.blocksZ) {
                             
                             Destroy (block.gameObject);
                             terrain.blockList.Remove(block);
                             
                         }
                         
                     }
                     
                 }
                 
             }
             
         } else if(!terrain.chunksToRender.Contains(transform)) {
             
             terrain terrainScript = blockGenerator.GetComponent<terrain>();
             terrainScript.renderChunk(transform);
             terrain.chunksToRender.Add(transform);
             
         }
unity-2012-09-05-17-23-10-14.jpg (8.6 kB)
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 aldonaletto · Sep 06, 2012 at 12:56 AM

You should not modify the list while iterating through it with foreach - this screws up the loop control, with unpredictable results. A good work around is to use a copy of the list in the foreach - this way you can safely remove elements from the original list. The List method [CopyTo][1] seems a good choice to make this copy:

      ...
      if(Vector3.Distance(chunkCenter,player.transform.position) > terrain.maxViewDistance) {
         terrain.chunksToRender.Remove(transform);
         // create a copy of blockList:
         Transform[] blocks = new Transform[terrain.blockList.Count];
         terrain.blockList.CopyTo(blocks); // copy the list to the blocks array
         foreach (Transform block in blocks) { // use blocks in the foreach...
           ...
           // now you can safely delete blockList elements in the foreach:
           terrain.blockList.Remove(block);
           ...
[1]: http://msdn.microsoft.com/en-us/library/t69dktcd.aspx
Comment
Add comment · Show 1 · 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 kag359six · Sep 06, 2012 at 07:44 PM 0
Share

Awesome. I wonder why I can't keep my framerate above 60 even with this whole method working. It runs between 25 to 60 fps which may sound good but isn't since im working on a gtx 560 ti. which means regular cards wont perform well. I'll make that a seperate question which I'd love for you to check out :)

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

8 People are following this question.

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

Related Questions

Code based 3d multiple cube instantiation 1 Answer

How to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer

Instantiate script crashes unity 3 Answers

Instantiate inside foreach loop 1 Answer

Character Select script issues 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