Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Coroutine over Loop

Discussion in 'Scripting' started by geetoo, Apr 26, 2011.

  1. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Hi.

    I would like to prevent some code to be executed until a certain condition is met. Basically, what I want to do is something as simple as
    Code (csharp):
    1. private bool isReady = false;
    2.  
    3. void Start()
    4. {
    5.     while (!someCondition)
    6.     {
    7.         // re-check my condition
    8.     }
    9.     isReady = true;
    10. }
    11.  
    12. void Update()
    13. {
    14.     if (isReady)
    15.     {
    16.         // do my stuff
    17.     }
    18. }
    Is there a nicer/more efficient way to do this, using a coroutine ?
    I tried converting my "void Start" into a "IEnumerator Start" and using
    Code (csharp):
    1. yield return someCondition;
    but didn't get much success so far.
    Thanks for the help.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you're checking every frame anyway, there's not much point having both a coroutine and Update; you can just do it in Update. However, wouldn't it be better to send a message when the condition is true and make a function run at that time, instead of constantly checking?

    --Eric
     
  3. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Thanks Eric.
    I'm still struggling a bit to understand exactly when and where to use coroutines. I guess in this case, it wasn't a good idea to use it.
    The SendMessage function wouldn't be much useful in my case, because the stuff I want to do when my condition is met is to be executed every frame. So I guess my Update check is the best solution to use.
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Generally co-routines make sense when you've got a task to perform which will take too long to perform in a single frame, without breaking your framerate badly. IE:

    Code (csharp):
    1. IEnumerator PerformTask (SomeSort ofParameter)
    2. {
    3.     while (!done  stillRelevant)
    4.     {
    5.         // do one frames work //
    6.         yield return null;
    7.     }
    8.  
    9.     if (done)
    10.     {
    11.         HandOver (result);
    12.     }
    13. }
    WaitForSecond and friends can be used if you don't need to update the work each frame or if you would like your coroutine to be re-called at a specific point in the call order.
     
  5. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    That or you want to control parameters over-time but it doesn't make sense to put your code in Update

    I sometimes do [pseudo-code]

    IEnumerator GoGetObject(GameObject object) {

    while(distanceToObject > 0) {
    transform.position = Vector3.Lerp(...)
    distanceToObject = Vector3.Distance(...)
    yield return 0;
    }
    grabObject(object);

    }