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

WaitForSeconds suspends the coroutine but does not unsuspend

Discussion in 'Scripting' started by NoFaceStudios, Jun 2, 2022.

  1. NoFaceStudios

    NoFaceStudios

    Joined:
    Jan 16, 2022
    Posts:
    3
    Hello,

    I have a coroutine that uses WaitForSeconds, the coroutine is run but the waitforseconds never unpauses, and nothing appears in the console, I've been stuck on this for about a week now, and can't seem to find a fix, though other scripts are working. Please help!
    Code (CSharp):
    1.     [SerializeField] GameObject FRWaterObj1;
    2.     bool WaterRiseStart;
    3.     int WaterRiseSpeed;
    4.     void Start()
    5.     {
    6.         StartCoroutine("WaitForWaterRise");
    7.         WaterRiseSpeed = 1;
    8.     }
    9.     IEnumerator WaitForWaterRise()
    10.     {
    11.         Debug.Log("Coroutine run");
    12.         yield return new WaitForSeconds(8);
    13.         Debug.Log("Suspend Over");
    14.         Water.WaterType = "Lava";
    15.         WaterRiseStart = true;
    16.         Debug.Log("WaterRiseStart set to True");
    17.  
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (WaterRiseStart)
    23.         {
    24.             Debug.Log("Water starting to rise");
    25.             FRWaterObj1.transform.position = new Vector3(FRWaterObj1.transform.position.x, FRWaterObj1.transform.position.y + (Time.deltaTime * WaterRiseSpeed), FRWaterObj1.transform.position.z);
    26.         }
    27.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So "Coroutine run" appears in the console but nothing afterwards? Is Time.timeScale set to 0?
     
  3. NoFaceStudios

    NoFaceStudios

    Joined:
    Jan 16, 2022
    Posts:
    3
    I tried setting Time.timeScale to 1 but the same thing happened, and yes "Coroutine run" does appear but nothing else does.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your code works just fine. You're going to have to do a thorough debugging to find out what is changing the time scale, disabling your script, destroying your GameObjects, or any other interactions you have that you might not realize. But your code works.

    water rise.png
     
  5. NoFaceStudios

    NoFaceStudios

    Joined:
    Jan 16, 2022
    Posts:
    3
    Somehow just got the code working after resetting the parent gameobject...
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    So coroutines, run on a object so if you disable the object you called StartCoroutine from, the coroutine will no longer tick along
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    It's actually even a little more subtle than that: "disable the object" is ambiguous and actually matters a lot:

    if you set the GameObject inactive, the coroutine is gone and done, never again to run.

    If you disable the MonoBehaviour, its Update() will stop, but its Coroutines continue running (!!!)

    See for yourself: put this on a GameObject and play in the editor.

    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. // @kurtdekker
    5. //    Use your editor window to explore the difference between:
    6. //        * setting the GameObject inactive / active
    7. //        --> coroutine is gone and done, never runs again
    8. //        * disabling / enabling this MonoBehaviour instance
    9. //        --> coroutine continues to run, while Update() does not
    10. //
    11. // You can make Start() instead be OnEnable() and see
    12. // different behaviour and even more confusion ensue!
    13.  
    14. public class Heartbeat : MonoBehaviour
    15. {
    16.     void Start()
    17.     {
    18.         StartCoroutine( HeartCoRo());
    19.     }
    20.    
    21.     IEnumerator HeartCoRo()
    22.     {
    23.         int beat = 0;
    24.         while(true)
    25.         {
    26.             beat++;
    27.             Debug.Log( "Heartbeat - " + beat);
    28.             yield return new WaitForSeconds(0.5f);
    29.         }
    30.     }
    31. }