Search Unity

Resolved Does StartCoroutine("x") restart 'x' or play a new 'x'

Discussion in 'Scripting' started by ArthurHaleta, Jan 1, 2022.

  1. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    I'm writing some code (which is irrelevant to the answer I would like) where I want to restart a Coroutine every time I call it. Does the Coroutine automatically restart or do I have to call another function to manually reset it and then start it again? I know the function StopCoroutine exists but just wanted to no if it was unnecessary.
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    No, you can call StartCoroutine multiple times but it will be executed multiple times. Each is it's own run.

    Yes, if you want to restart a coroutine you can keep a reference to the coroutine when you call
    StartCoroutine
    . Then use
    StopCoroutine
    and clean up the values it might have changed. Then call
    StartCoroutine
    again.
     
    ArthurHaleta likes this.
  3. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    Thanks! It works