Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Milenchy · May 15, 2016 at 06:47 PM · coroutineloop

Loop in coroutine stops after going halfway

I have a very weird problem with a particular loop in a coroutine:

     public IEnumerator DestroyCubesGameOver() {
     var startingCubeHolder = GameObject.FindWithTag("StartingCubeHolder");
     DestroyOutOfCameraCubes();

     Debug.Log(cubeGroup.transform.childCount);

     for (int i = 0; i < cubeGroup.transform.childCount; i++) {
         Debug.Log(i);
         Destroy(cubeGroup.transform.GetChild(i).gameObject);
         yield return new WaitForSeconds(0.001f);
     }

     Destroy(startingCubeHolder);
 }

The loop starts doing its thing and it needs to do it 100 times for example. However it stops after going about halfway and after destroying about half of the objects. Even if I increase the delay for each repeat it still does the same exact thing - needs to repeat itself and destroy 100 objects, ends after destroying just 50. What is causing this?

Edit: If I make it just a void method it destroys all of the objects.

Edit 2: This loop was initially a foreach but I rewrote it to try the other way - same thing.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Milenchy · May 15, 2016 at 10:27 PM

This was the problem: on each loop I destroy one object so the childCount goes down. At the half my i and childCount meet and the loop stops.

Fixed it by setting the objects as inactive at first so that they can disappear from the player's view and then destroying them without a coroutine later.

Comment
Add comment · 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
0

Answer by MosoWare · May 15, 2016 at 09:09 PM

Try using gameObject.SetActive(false) instead of Destroy(object). What this will do is hide all of the objects in your scene by setting their 'enabled' property to false. This is technically the same thing as destroying an object because none of the scripts will fire on a disabled object.

If you absolutely need to destroy the objects, then I suggest trying to set the WaitForSeconds much higher, to about 1 or 2 full seconds. It isn't good practice to destroy a large amount of objects quickly. Even if it works, you're stuck with a bunch of dropped frames and lag :(

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 Milenchy · May 15, 2016 at 09:30 PM 0
Share

It's a one time thing during the end of the game where all of the objects need to be removed so it's not a big deal if there are a couple of stutters. But I still don't understand why the coroutine skips loops.

avatar image MosoWare · May 15, 2016 at 09:32 PM 0
Share

I've experienced similar behavior when executing expensive code and my solution has been to try to make it less expensive. Did the 1 or 2 second delay work ins$$anonymous$$d of the .0001 second delay?

avatar image Milenchy MosoWare · May 15, 2016 at 10:32 PM 0
Share

I found the problem, made an answer.

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

55 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Coroutines Madness (); 1 Answer

What is the best way to loop with very short and accurate interval? 0 Answers

Nested coroutines 0 Answers

What to do for many comparison operations in foreach loop? 0 Answers

Round Loop Problem?... 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