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
2
Question by Reverend-Speed · Apr 04, 2012 at 01:35 AM · c#crashcoroutineienumeratorwhile

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

So - I see there are numerous questions relating to this issue, but I've been unable to find one that scratches this particular itch... or if I have, I'm not savvy enough to recognise it. Please be patient - and if you know of a previously answered question that can clear my problem up, please send it right along. =)

I'm executing a while loop which iterates through an array - but as this naturally crashes Unity, I'm trying to execute a coroutine which will slow the execution to once every 4 seconds (in this case).

In theory: The While loop starts, performs the actions in the For section, encounters StartCoroutine(WaitASec (4.0F)); , bounds off to IEnumerator WaitASec, pauses for 4 seconds, then evaluates the While condition and starts again as long as loophandle is true.

In practice: CRASH.

Using Debug.Logs and inserting the line 'loophandle = false' at the end of the While loop seems to indicate that the program reaches the Debug.Log line 'At end of While Loop' BEFORE the Debug.Log line 'Waited a sec'.

So - the newbie is baffled.

Right, code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CameraControllerRevCsharp : MonoBehaviour {
 
         ...
 
     //Boolean switch to turn the tracking on and off
     public bool loophandle = true;
 
         ...
 
     public void Start() {
         
         Transform target = GameObject.Find("Player").transform;
             while (loophandle){
                 Debug.Log ("Started While Loop");
                 if(cameraNodes.Count > 0){
                     int node = 0;
                     for (int i = 0; i < cameraNodes.Count; i++){
                         Debug.Log ("Started for section");
                         if(cameraNodes[i].range >= Vector3.Distance(target.position, cameraNodes[i].position))
                         {
                             node = i;
                         }
                     }
                     Debug.Log ("About to transform camera");
                     transform.position = ( (CameraNode)cameraNodes[node]).position;
                 }
                 Debug.Log ("Calling StartCoroutine");
                 StartCoroutine(WaitASec(4.0F));
                 // FOR TESTING: loophandle = false;
                 Debug.Log ("At end of While Loop");
         }
     }
     IEnumerator WaitASec(float waitTime){
         yield return new WaitForSeconds(waitTime);
         Debug.Log ("Waited a sec");
     }
 
     public void Update (){
         transform.LookAt(target);
         Debug.Log ("Looking at target");
     }
         
 }

Thanks for your brain time on this. Here's hoping you can phrase the solution in plain enough language so I never end up doing this bloody stupid thing again. =)

--Rev

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

4 Replies

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

Answer by by0log1c · Apr 04, 2012 at 01:47 AM

Hey there again, I just realized the error while going over the comments on the last question. A while loop need to yield a pause every now and then or it'll crash the game.

You may do it like this:

 while(true)
 {
     DoSomething();
     yield return null; //wait for a frame
     yield return new WaitForSeconds(3.14f);
 }

EDIT: I notice you actually call a coroutine that does the waiting, that could work but you need to wait for that method to return after itself waited after the yield to return ;)

You'd do something like:

 //method return type has to be IEnumerator
 IEnumerator Start()
 {
     yield return StartCoroutine(MyDelayMethod(3.1415f));
     //3.1415 seconds later
 }
 
 IEnumerator MyDelayMethod(float delay)
 {
     yield return new WaitForSeconds(delay);
 }
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 Reverend-Speed · Apr 04, 2012 at 11:30 AM 0
Share

Again, excellent answer! $$anonymous$$uch obliged - I've cleared the old errors from my console and this script is working just fine.

Naturally, I have a new problem now, related to cs0176... and after a couple of hours trying to solve the issue, it turns out that I can't find an answer on this site.

Assu$$anonymous$$g that it's better to be productive than spin my wheels, I'm going to submit another question and get back to work on Blender. =)

Thanks muchly for your time!

(I'll just cough leave this here... http://answers.unity3d.com/questions/235285/cs0176-trying-to-locate-the-main-camera-in-order-t.html )

avatar image StalikVonDark · Jul 27, 2020 at 09:25 AM 0
Share

Still relevant today - you need to use a yield return null in conjunction with the yield return new WaitForXXX.

Apparently using just yield return new WaitForXXX variants fails to process. After much debugging the phantom yield return null was the secret here.

Great catch!

avatar image
0

Answer by josetgg · Nov 07, 2012 at 11:16 PM

Just curious, why are you running a while loop on the start function?

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 Berenger · Apr 04, 2012 at 02:26 AM

Indeed, calling a coroutine without yielding it's return value in a while() is just going to start a bazillion (and that's a lot !) instances of coroutines and freeze the program.

Comment
Add comment · Show 1 · 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 Reverend-Speed · Apr 04, 2012 at 11:32 AM 0
Share

You would suggest it's better to keep the number of coroutines under a gahzillion, then?

Thanks for the explanatory note - believe me, I need the support (just starting C#).

avatar image
0

Answer by VillainT_ · Nov 27, 2012 at 02:47 PM

You can use (for javascript)

 while (condition) {
 //codes
 yield WaitForSeconds (0);
 }

http://forum.unity3d.com/threads/160337-How-to-use-while-loop-without-crashing-the-game?p=1096760#post1096760

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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IEumerator not recognising when a bool has switched 1 Answer

How do I get into the data returned from a UnityWebRequest ? 2 Answers

Unity Editor Completely Freezing on Server Start 1 Answer

Multiple Cars not working 1 Answer

How to solve while loop lags 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