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 Anxo · Nov 02, 2012 at 03:58 AM · coroutineienumeratorwhilewhile loop

Problem with while loop in Coroutine

Hey Unity Community.

I hope all is well.

I am having a brain fart and can not seam to be able to figure out why this simple test would not work.

 void Start(){
 StartCoroutine("whileTest");
 }
 
 IEnumerator whileTest()
     {
         while(true == true)
         {
             
             yield return new WaitForSeconds(1);
             Debug.Log ("while test working");
         }
         
     }

What could possibly be the reason why that Debug.log would not show up every second? I checked the time.time by debug.logging it into the update and it works fine. And if I place the Debug.log infront of the yield, it prints. But it never gets past the Waitforseconds.

Thanks all.

Comment
Add comment · Show 11
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 Anxo · Nov 02, 2012 at 04:00 AM 0
Share

note that 1.0f does not improve the situation

avatar image Anxo · Nov 02, 2012 at 04:05 AM 0
Share

Not that I am using the method in the following script and it works great.

IEnumerator Flip() { while(true) { ranFloat = Random.Range (0.0f, 50.0f); randomRot.x = Random.Range (1.0f,2.0f) - 1.0f; randomRot.y = Random.Range (1.0f,2.0f) - 1.0f; randomRot.z = 0.0f; // Debug.Log (randomRot); flipSwitch = true; yield return new WaitForSeconds(2.0f); randomRot = Vector3.zero; flipSwitch = false; yield return new WaitForSeconds(2.0f); }

}

avatar image Anxo · Nov 02, 2012 at 04:48 PM 0
Share

This is not an answer! I am posting this into answers because commenting does not have formatting for code. This is what I found.

if I run...

 void Start(){
 StartCoroutine(Generate());
 }

it runs through the loop fine and I see all the debugs.log in the loop multiple continuously.

 IEnumerator Generate(){
         Debug.Log ("Generator is running for "+gameObject.name);
         while(true)
         {
             Debug.Log ("new loop in generator");
             yield return new WaitForSeconds(0.5f);
             //myParent.Broadcast$$anonymous$$essage("ChangePower",5.0f);    
             Debug.Log("we got past the 5 second wait");    
         }
     }

does what one would expect. But then if I call that same function out of here:

 void SetGenerator (bool incBool)
     {
         Debug.Log ("set generator has been set to "+incBool+ " for " +gameObject.name );    
         if(incBool == true)
             StartCoroutine(Generate());
         
     }

It does get into the function, it prints out the "Generator is running" log and the "new loop in generator" log so it gets inside the "while" loop but it never prints the "we got past the 5 second wait"

What em I missing here.

avatar image Anxo Anxo · Nov 02, 2012 at 05:02 PM 0
Share

No Errors, it gets worse. Once I tried to run the Coroutine from the SetGenerator function it breaks the script permanently. Now even if I comment everything out of the script and only have the start function saying StartCoroutine(whiletest());

And the whiletest has nothing but a loop like while(true) { Debug.log(time.time); yield return new WaitForSeconds(1); } (caps and and misspelling are not in the script just in the comment)

It no longer works. I have to make a new script to get it to work again.

avatar image Anxo · Nov 02, 2012 at 04:51 PM 0
Share

I am sure the incBool is true because the Debug.log before the check tells me so, in addition it prints the first 2 debug.logs before the yield instruction.

avatar image liszto · Nov 02, 2012 at 04:56 PM 0
Share

And your console say no error or something like that :/ That's weird for me it must work... I'm investigating ^^"

Show more comments

2 Replies

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

Answer by Demigiant · Nov 02, 2012 at 11:41 PM

Only thing that comes to my mind (apart what Eric5h5 said) is that somehow the MonoBehaviour that started the coroutine was destroyed. Did you check it? Coroutines live and die with the MonoBehaviour that starts them, not with the MonoBehaviour that contains them.

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 Anxo · Nov 03, 2012 at 01:05 PM 0
Share

wow, you actually got me there. I did not think that it could possibly be the case because I never delete the game object but on initialize, it turns all children off for a second and then turns on only the object that it needs to. So what happens is that it runs and gets turn off and on again. So to me it does not look like the object is deleted or interrupted but I 4got that I has setup to the setup stage that way. I was so stuck that I thought Unity was going loco so I had reinstalled unity and everything. Thank you for helping me brain storm. Thank you too for your effort Eric. I have not had the need to be back here in a while, I can usually find what I need now but this was driving me loco. I love that Unity has such a strong community.

avatar image Demigiant · Nov 03, 2012 at 01:43 PM 0
Share

Ehe, glad I could help with the brainstor$$anonymous$$g. I know how it is when something feels so "sure" that you kick your brains while trying to find the issue elsewhere ;)

avatar image Myrddraal · Jan 24, 2014 at 02:19 PM 1
Share

I was having a similar problem, and this answer also solved the problem for me! Sorry I can't vote it up (not enough rep)

avatar image dogwynn · Nov 01, 2015 at 08:28 PM 0
Share

Just to add a little extra information to this answer, I had a problem with an infinite Coroutine/thread being killed as well. $$anonymous$$y problem was that I placed the call to StartCoroutine in the Awake method and not the Start method. As soon as I moved to to the Start method, it fixed the problem.

avatar image TheGreatCabbage · Mar 22, 2017 at 05:37 PM 0
Share

Thank you! I spent so long wondering why my coroutine wasn't working, this is the solution.

avatar image
2

Answer by Eric5h5 · Nov 02, 2012 at 05:33 AM

Turn off "collapse" in the console. (Number of times I've typed this answer: 500 and counting. ;) ) By the way, you should generally not use quotes in StartCoroutine unless you intend to use StopCoroutine.

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 Anxo · Nov 02, 2012 at 12:22 PM 0
Share

Sorry to frustrate you Eric, you have answered many of my questions in the past. But this is not the case. Collapse is not on. There must be something in the rest of the script that is stopping my Coroutine. I have checked for return;s but do not see one that could make the difference. I am going to comment out one line at a time to see where the problem is.

avatar image Anxo · Nov 02, 2012 at 04:41 PM 0
Share

Ok I have an interesting development with this problem. I started a new scrip with just the while loop test which works fine. I set one up that would just print the time every second. Then I started copy and pasting in one element at a time into the test script to see where to problem lies. What that helped me to find was that now I can run the none working loop from the "Start" function but not from the function that it needs to be called from. I am going to post what I mean in an answer because comments do not have formatting for code.

avatar image Eric5h5 · Nov 02, 2012 at 04:48 PM 1
Share

Well, the code you posted works fine and the only possible reason for the Debug.Log not showing up every second in this case is if collapse is turned on.

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

16 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

Related Questions

Problems with While Loops Only repeating Once 1 Answer

c# Using an IEnumerator yield WaitForSeconds to temporarily pause a While loop 3 Answers

Stop Current Coroutine, Then Restart It 1 Answer

How to wait for Coroutine to finish? 2 Answers

Coroutine WaitForSeconds is varying? How to fix? 3 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