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 cmo · Apr 03, 2014 at 10:45 AM · coroutinefor-loop

For loop getting skipped/doesnt work

Here's the scenario: I've got a simple for loop in a coroutine that was working perfectly, and then suddenly stopped evaluating.

Here's what is looks like, with some debug messages I added in:

     public static IEnumerator CamGoHome () {
 
                 // do stuff
 
         yield return new WaitForSeconds (flyTime);
 
         Debug.Log ("hi1");
 
         for (int i = 20; i <= 0; i--) {
             Debug.Log ("hi2");
             grey.renderer.enabled = !grey.renderer.enabled;
 //            yield return new WaitForSeconds (0.5f * i);
         }
 
         Debug.Log ("hi3");
 
         // do other stuff
     }

So, this worked before: the renderer of grey blinked, and this worked in multiple tests. I fiddled with the yield return timing in the for loop, saved, and ... the for loop suddenly stopped evaluating.

I've changed the int used in the for loop, the parameters, cleared the loop out completely. I've deleted it and rewritten it. The coroutine always completes (I see the "hi1" and "hi3" messages in the console every time) but it seems to just completely ignore the for loop, or any new for loop of any kind written into the same section of the routine. If I write a for loop up above, it does work, but in that particular section of code it's just invisible.

Anyone know what would cause this?

Comment
Add comment · Show 2
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 flamy · Apr 03, 2014 at 10:49 AM 0
Share

// yield return new WaitForSeconds (0.5f * i);

uncommment this line....

Actually it is working now also but everything happens in one frame, so you cant see the change.

avatar image cmo · Apr 03, 2014 at 10:59 AM 0
Share

Sorry, not relevant - I just had it commented out at that particular moment. The debug messages would appear in the console regardless.

1 Reply

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

Answer by flamy · Apr 03, 2014 at 11:07 AM

 for (int i = 20; i <= 0; i--)


this line is the problem, especially the condition. you are checking if i is less than or equal to 0 at the begining obviously 20

try

 for (int i = 20; i >= 0; i--)   //for 21 times
 for (int i = 20; i > 0; i--)   //for 20 times


it is always better to keep the counts increasing rather than decreasing, unless you have the absolute need to do so!

 for (int i = 0; i < 20; i++)   //for 20 times


Comment
Add comment · Show 2 · 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 cmo · Apr 03, 2014 at 11:31 AM 0
Share

I have to apologize flamy - you're being very helpful but I really screwed up the code I pasted in. I was getting frustrated and just typing in various values. $$anonymous$$y starting values for the code were actually the same as what you suggested, and that's what originally stopped working.

So, it really boggled me when I pasted in your answer and... it worked ;) However, it still had a really weird problem - ins$$anonymous$$d of evaluating 20 times, it evaluated 3-4 times.

The problem appears to be in the yield returns. I don't have a particularly good understanding of what's going on, but it looks like yields that are too short cause the for loop to end - maybe because the time fraction is shorter than a frame? Anyway, that would explain why my initial change to the yield is the point at which the for loop stopped evaluating.

I'm going to poke at it some more then post an answer, I guess. Weird problem!

avatar image cmo · Apr 04, 2014 at 03:25 AM 1
Share

And.. I never figured this out. It all now works as if nothing ever happened. I'm going to mark your answer as correct just in case I was having a massive, long-running brain fart and repeatedly messed up the for loops (although I'd swear the moment it stopped working is when I changed the yield ti$$anonymous$$g...)

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

21 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

Related Questions

changing a coroutine's argument at the coroutine's runtime? 1 Answer

Run coroutine X amount of times in Y seconds? 3 Answers

Understanding yield inside a for or a while loop 2 Answers

Why is unity exiting a loop inside a coroutine after its first run through? 1 Answer

A loop FOR waits for a coroutine to end 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