Search Unity

WaitForSeconds Not Working

Discussion in 'Scripting' started by TheWatcherYouTube, May 7, 2019.

  1. TheWatcherYouTube

    TheWatcherYouTube

    Joined:
    May 7, 2019
    Posts:
    2
    WaitForSeconds only runs through the first enumeration, then stops and doesn't run through the rest of the text.

    Code (CSharp):
    1. IEnumerator Cassette2()
    2.         {
    3.  
    4.             print(Time.time);
    5.             Cassette1Found.text = "I'm being watched,";
    6.             yield return new WaitForSeconds(5);
    7.             Cassette1Found.text = " I'm being watched by some delusional freak.";
    8.             yield return new WaitForSeconds(5);
    9.             Cassette1Found.text = " [Bang on tent] ";
    10.             yield return new WaitForSeconds(5);
    11.             Cassette1Found.text = "DANG YOU!";
    12.             yield return new WaitForSeconds(5);
    13.             Cassette1Found.text = " GET AWAY!";
    14.             yield return new WaitForSeconds(5);
    15.             Cassette1Found.text = " IM NOT DYING TODAY!";
    16.             yield return new WaitForSeconds(5);
    17.             Cassette1Found.text = " No, no, no, no, no, NO!";
    18.             yield return new WaitForSeconds(5);
    19.             Cassette1Found.text = " GET THE HECK OUT OF HERE!";
    20.             yield return new WaitForSeconds(5);
    21.             Cassette1Found.text = " [Dies]";
    22.             yield return new WaitForSeconds(5);
    23.             Cassette1Found.text = "";
    24.             print(Time.time);
    25.  
    26.         }
     
  2. It works for me:
    hub.PNG
    Calling through this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         StartCoroutine(Cassette2());
    11.     }
    12.  
    13.     IEnumerator Cassette2()
    14.     {
    15.         print(Time.time);
    16.         Debug.Log("I'm being watched,");
    17.         yield return new WaitForSeconds(5);
    18.         Debug.Log(" I'm being watched by some delusional freak.");
    19.         yield return new WaitForSeconds(5);
    20.         Debug.Log(" [Bang on tent] ");
    21.         yield return new WaitForSeconds(5);
    22.         Debug.Log("DANG YOU!");
    23.         yield return new WaitForSeconds(5);
    24.         Debug.Log(" GET AWAY!");
    25.         yield return new WaitForSeconds(5);
    26.         Debug.Log(" IM NOT DYING TODAY!");
    27.         yield return new WaitForSeconds(5);
    28.         Debug.Log(" No, no, no, no, no, NO!");
    29.         yield return new WaitForSeconds(5);
    30.         Debug.Log(" GET THE HECK OUT OF HERE!");
    31.         yield return new WaitForSeconds(5);
    32.         Debug.Log(" [Dies]");
    33.         yield return new WaitForSeconds(5);
    34.         Debug.Log("cv");
    35.         print(Time.time);
    36.  
    37.     }
    38. }
    39.  
     
  3. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    WaitForSeconds is using Time.TimeScale if your time scale is zero you need to use WaitForSecondsRealtime

    or maybe you're calling StopAllCoroutines(); in somewhere your code?
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Did you make sure you invoked the function using StartCoroutine() rather than just calling it directly?
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Also make sure you're not calling StartCoroutine in Update or something similar, creating a new coroutine every frame, as that will appear to never advance as well.
     
  6. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Please for the love of god, DRY man, DRY

    Code (CSharp):
    1. IEnumerator Cassette2()
    2.         {
    3.            var sentences = new[]
    4.            {
    5.                  "I'm being watched,",
    6.                  "I'm being watched by some delusional freak.",
    7.                  "[Bang on tent] ",
    8.                  "DANG YOU!",
    9.                  "GET AWAY!",
    10.                  "IM NOT DYING TODAY!",
    11.                  "No, no, no, no, no, NO!",
    12.                  "GET THE HECK OUT OF HERE!",
    13.                  "[Dies]",
    14.            };
    15.  
    16.            var wait = new WaitForSeconds(5);
    17.            foreach(var sentence in sentences)
    18.            {
    19.               Cassette1Found.text = sentence;
    20.               yield return wait;
    21.            }
    22.  
    23.         }
     
    Joe-Censored likes this.
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    He's new. Hope you're prepared to explain your code line-by-line for him. :p
     
    AndersMalmgren likes this.
  8. I didn't bother with this because rules:
    1; have working code base
    2; have performant code base
    3; refactor on ways your code becomes more readable and maintainable

    And OP has problems with the very first point, no meaning trying to move on the others, it becomes confusing for them real quick.
     
    Munchy2007 likes this.