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 Avash · Oct 23, 2014 at 09:51 AM · c#crashcrashing

for loop crash...

 Vector3 gridStart = transform.position - new Vector3(worldX, worldY, worldZ);
                 for(float x =0F; x<worldX; x += snap) {
                         Debug.Log (x);
                         for(float y =0F; y<worldY; y += snap) {
                                 for(float z =0F; z<worldZ; z += snap) {
                                         GameObject gridCube = Instantiate(prefab) as GameObject;
                                         Vector3 startPosition = transform.position + new Vector3(worldX, worldY, worldZ);
                                         gridCube.transform.position = new Vector3(gridStart.x + x, gridStart.y, gridStart.z + z);
                                         gridCubesList.Add(gridCube);
                                         gridCubes = gridCubesList.ToArray();
                                 }
                         }
                 }

So this code totally freezes my editor. What's the problem?

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

Answer by gjf · Oct 23, 2014 at 09:51 AM

what are the values of worldX/Y/Z? if they're not small, it's likely that it's just taking a very long time...

... especially if you're converting the list to an array EVERY iteration...

you don't need to do that at all - you can index the list like an array.

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 Bunny83 · Oct 23, 2014 at 10:26 AM 1
Share

@Avash:
Yep it depends on your worldXYZ size and what "snap" is set to.

Example if your world is 10 / 10 / 10 and snap is 1 you will have 1000 iterations and thousand cubes (that will already take quite a while).

But if you would use 0.1 for "snap" you will have 1000000 iterations and if it's 2 you only have 125 iterations. Also since you'll probably end up with a lot of elements in your list you should set the list's Capacity to the number of elements expected before the loop, so it doesn't get resized. If you really need the list as array, do it after the loop, once.

Should that be something like $$anonymous$$ecraft? If so you're completely on the wrong track ;) You can't instantiate millions / billions single cubes.

avatar image Avash · Oct 24, 2014 at 09:54 AM 0
Share

They're all ten and snap is 0.2.

avatar image Avash · Oct 24, 2014 at 11:05 AM 0
Share

Is 1000 cubes too much?

avatar image Avash · Oct 24, 2014 at 11:08 AM 0
Share

Damn I think I need them all...

avatar image Avash · Oct 26, 2014 at 10:09 AM 0
Share

I'm just trying to code a grid solution... I want it to work with big objects.

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

Unity crashing after pressing CTRL D on gameobject or trying to make into prefab!? 0 Answers

IOS crashes with an EXC_BAD_ACCESS on a thread running com.apple.avfoundation.videodataoutput.bufferqueue 0 Answers

Unity freezes sometimes when I'm tabbed out. 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