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

Question I can't stop my coroutine.

Discussion in 'Scripting' started by Johnscaban, Dec 28, 2021.

  1. Johnscaban

    Johnscaban

    Joined:
    May 9, 2020
    Posts:
    23
    I have this coroutine:
    Code (CSharp):
    1. private IEnumerator CountDownToStartGame()
    2. {
    3.     yield return new WaitForSeconds(countdownToStartGame); //Lasts 10 seconds
    4.  
    5.     StartCoroutine(StartGame());
    6. }
    I start it like this:
    Code (CSharp):
    1. countdownCoroutine = StartCoroutine(CountDownToStartGame());
    If something happens, I call this method:
    Code (CSharp):
    1. public void StopStartingGame()
    2. {
    3.     StopCoroutine(countdownCoroutine);
    4.  
    5.     Debug.Log("Coroutine stopped");
    6. }
    But its not working, how can I stop it?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Coroutines are NOT always an appropriate solution: know when to use them!

    https://forum.unity.com/threads/things-to-check-before-starting-a-coroutine.1177055/#post-7538972

    Generally, if your design requires you to call StopCoroutine(), then don't make it a coroutine.

    It sounds like you really need something like a cooldown timer, and you want to interrupt it.

    This means a timer that without further intervention will cause something to happen in X seconds.

    But... things can happen that extend that time (or stop the count entirely), usually by resetting the timer.

    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297
     
  3. Johnscaban

    Johnscaban

    Joined:
    May 9, 2020
    Posts:
    23
    I'm making a multiplayer game and when I client joins while the game is starting, I stop the countdown coroutine. I want it like that to avoid errors and to let the new player vote for the map and game-modes. Should I not use a coroutine?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I wouldn't. Once start is pressed, set a timer to 10 seconds and count it down.

    When players join, either stop the count or reset the count to 10 seconds.

    There's just not a lot of ways this code can go bad:

    Code (csharp):
    1. // zero means don't start, nonzero means start in that amount of time.
    2. float timeToStart;
    3.  
    4. void Update()
    5. {
    6.   if (timeToStart > 0)
    7.   {
    8.     timeToStart -= Time.deltaTime;
    9.     if (timeToStart <= 0)
    10.     {
    11.        // call the start game here
    12.     }
    13.   }
    14. }
    15.  
    16. void ResetCountdown()
    17. {
    18.   timeToStart = 10;
    19. }
    20.  
    21. void StopCountdown()
    22. {
    23.   timeToStart = 0;
    24. }
     
    Last edited: Dec 28, 2021
    davidnibi likes this.