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 stuart6854 · Oct 11, 2013 at 03:55 PM · instantiatedelaycrashinglooping

Instantiate Loop Crashing

Hello,

I have created a loop to spawn in cubes to a certain depth below some Perlin-Noise terrain. The loops will freeze though when i don't have any of the below in it:

 yield WaitForSeconds(0.1);

But makes the blocks spawn in too slowly. Is their anyway to make all the blocks spawn in without crashing Unity?

Code:

 for(var child : Transform in transform){    //Creating layers of blocks below the initial layer 
         var level = child.transform.position.y - 1;
         
         while(level > -6){
             var cb = Instantiate(cubeTwo, Vector3(child.transform.position.x, level, child.transform.position.z), Quaternion.identity);
             cb.transform.parent = transform;
             level -= 1;
             Debug.Log(level);
             
         
         }
         yield WaitForSeconds(0.001);    //Wait 1 Frame
     }

Any advice is welcomed :)

Comment
Add comment · Show 6
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 fafase · Oct 11, 2013 at 04:07 PM 0
Share

What is the purpose of waiting for one frame? you may use

 yield null;

for the same principal not sure about Js syntax, it is yeild return null; in C#.

But I don't get the point of waiting for so few, you might as well just make it happen at once.

avatar image ArkaneX · Oct 11, 2013 at 04:07 PM 0
Share

Ins$$anonymous$$d of instantiating here, do it earlier (just after user starts your game) and keep inactive cubes in a generic list. And when needed, just activate them, move to desired position and set parent.

avatar image ArkaneX · Oct 11, 2013 at 04:11 PM 0
Share

@fafase - I think you're referring to while loop, but there's also a for loop. I guess the main transform has many children.

avatar image stuart6854 · Oct 11, 2013 at 04:16 PM 0
Share

@ArkaneX - This loop happens in the

 function Start () {}
avatar image ArkaneX · Oct 11, 2013 at 04:22 PM 0
Share

@stuart6854 - I thought so :) But I'm writing about creating cubes earlier.

Let's say, an user runs your game. He sees some splash screen, then some start screen, and then after he hits play, your terrain is displayed and cubes are generated. $$anonymous$$y advice is to create cubes earlier, e.g. in start screen, using for example empty object with generation script attached. When user hits Play, you can retrieve cubes and do anything you want with them.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Oct 11, 2013 at 04:24 PM

Your problem is that what you do actually shouldn't work and should throw an exception, however the enumerator which Unity implements for the transform class doesn't do an internal version check which it should!

What happens is that you iterate over the child objects of the transform, but inside the loop you add more childs. That results in an infinite loop. You should save the transform list before you iterate over it.

In C# i might use Linq to pull a copy of the child array, however i'm not sure if and how it works in UnityScript.

That however should work:

 var childs = new Transform[transform.childCount];
 var index = 0;
 for(var tmp : Transform in transform){
     childs[index++] = tmp;
 }
 for(var child : Transform in childs){
     var level = child.transform.position.y - 1;
     while(level > -6){
         var cb = Instantiate(cubeTwo, Vector3(child.transform.position.x, level, child.transform.position.z), Quaternion.identity);
         cb.transform.parent = transform;
         level -= 1;
         Debug.Log(level);
     }
 }

If you don't want a delay you don't need a yield (as long as the loop has an end ;) )

Comment
Add comment · Show 3 · 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 stuart6854 · Oct 11, 2013 at 04:29 PM 0
Share

Is that why it crashes without waitforseconds (because of infinite loop?)?

avatar image Bunny83 · Oct 11, 2013 at 04:30 PM 0
Share

Yes, of course

avatar image stuart6854 · Oct 11, 2013 at 04:38 PM 0
Share

Wow,

that code worked, except any size bigger than 10x10 freezed the game for about 5 seconds then works, but i guess i could hide that in a loading screen.

Thanks :)

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

17 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

Related Questions

How to spawn objects but set a cap of how many can be alive at the same time? 0 Answers

how to make an enemy fire a bullet once every second 4 Answers

Delay Collecting Object by Player 0 Answers

Delay a prefab instantiate 5 Answers

When does Instantiate return ? 2 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