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. Dismiss Notice

Coroutine not looping

Discussion in 'Scripting' started by lewdoo, Jan 18, 2021.

  1. lewdoo

    lewdoo

    Joined:
    Sep 7, 2018
    Posts:
    9
    Could somebody explain why size increments to 1 and stops??

    Code (CSharp):
    1. private IEnumerator MyCoroutine()
    2.     {
    3.         while(size < 10)
    4.         {
    5.             size++;
    6.             yield return null;
    7.         }
    8.  
    9.         yield return new WaitForEndOfFrame();
    10.         Debug.Log("full size");
    11.     }
    12.  
     
  2. JoshWindsor

    JoshWindsor

    Joined:
    Jun 2, 2018
    Posts:
    24
    Have you tried using if, instead of while?

    Also have you used startcoroutine(MyCouroutine()); earlier in the script?

    There might also be a need to use a variable to determine the speed at which Size increases, every time I use ++, the value increases super fast.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    As suggested, make sure you are calling your coroutine correctly. Also note, yield return null waits one frame, which means your while loop will go super fast overall.

    @JoshWindsor The while statement makes more sense here if OP wants a loop to increase size based on the yield time and loop. An if statement would only increment it once when the coroutine is called.
     
  4. JoshWindsor

    JoshWindsor

    Joined:
    Jun 2, 2018
    Posts:
    24

    That makes sense :) I haven't been using while loops so that's why I suggested an if statement. I think I will do some research and start using them myself. Tyvm!
     
  5. lewdoo

    lewdoo

    Joined:
    Sep 7, 2018
    Posts:
    9
    @JoshWindsor The while statement makes more sense here if OP wants a loop to increase size based on the yield time and loop. An if statement would only increment it once when the coroutine is called.[/QUOTE]


    Hey, yeah I'm using start coroutine so should all be good there. Not concerned about the speed of the loop. This was just a snippet to simplyfy and demonstrate my problem
     
    Last edited: Jan 18, 2021
  6. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Your snippet works as expected for me, "full size" is logged when
    size
    is 10.
    • Maybe the game object your coroutine is running on is destroyed and therefore the coroutine stopped?
    • Maybe something else is changing
      size
      while the coroutine is running?
    • Or your actual code is subtly different to the snipped you posted?
     
    lewdoo, Joe-Censored and Bunny83 like this.
  7. GGfazo

    GGfazo

    Joined:
    Sep 18, 2020
    Posts:
    3
    Maybe you're using StartCoroutine() in Start() instead of Update()
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Why would you use it in Update? That's bad advice considering the posted code. Not only would that start up the coroutine every frame, thus quickly increasing the value of size due to multiple coroutines incrementing it, it also would continue to run the coroutine beyond it's use.
     
  9. lewdoo

    lewdoo

    Joined:
    Sep 7, 2018
    Posts:
    9
    Solved it, thanks for the suggestions, tried running the code in a isolate script on a new gameObject and ran fine.

    Another script was disable and re-enabling the object, interrupting the coroutine
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Interesting, I didn't know a coroutine's execution could be stopped by disabling it's associated GameObject.
     
    abhi1abhisheksaha likes this.
  11. lewdoo

    lewdoo

    Joined:
    Sep 7, 2018
    Posts:
    9
    I don't know if was the object being disabled, but it was the child of a child of a child etc that had the script attached, and something was happening on the way