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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

IEnumerator timer not functioning

Discussion in 'Scripting' started by GreenBoxInteractive, May 3, 2015.

  1. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    I have this code, when gravbar = true, the IEnumirator timer should enable a gameobject, wait 10 seconds then disable it again, but nothing happens when gravBar = true;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public static int lives;
    7.     public static bool doubleScore;
    8.     public static bool gravBar;
    9.     public GameObject gravBarObject;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         gravBarObject.SetActive (false);
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.     if (lives > 3 ) {
    19.             lives = 3;
    20.         }
    21.  
    22.     }
    23.     IEnumerator GravBar ()
    24.     {
    25.         if (gravBar = true) {
    26.             gravBarObject.SetActive (true);
    27.             yield return new WaitForSeconds (10f);
    28.             gravBarObject.SetActive (false);
    29.         }
    30.     }
    31.  
    32. }
    33.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Nowhere are you calling StartCoroutine.
     
    GreenBoxInteractive likes this.
  3. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Ooohhh, I forgot about that.
     
  4. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    I am unfamiliar with the syntax of Corutines and IEnumerators, is it something like this?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public static int lives;
    7.     public static bool doubleScore;
    8.     public static bool gravBar;
    9.     public GameObject gravBarObject;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         gravBarObject.SetActive (false);
    14.  
    15.  
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.     if (lives > 3 ) {
    21.             lives = 3;
    22.         }
    23.         if (gravBar = true) {
    24.             StartCoroutine (GravBar);
    25.         }
    26.     }
    27.     IEnumerator GravBar ()
    28.     {
    29.             gravBarObject.SetActive (true);
    30.             yield return new WaitForSeconds (10f);
    31.             gravBarObject.SetActive (false);
    32.         }
    33.  
    34.  
    35. }
    36.  
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yup. You just want to be careful with making sure you don't start a new coroutine every frame.
     
    GreenBoxInteractive likes this.
  6. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    I get errors.

    Assets/Scripts/GameManager.cs(28,25): error CS1502: The best overloaded method match for `UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)' has some invalid arguments


    Assets/Scripts/GameManager.cs(28,25): error CS1503: Argument `#1' cannot convert `method group' expression to type `System.Collections.IEnumerator'
     
  7. Deleted User

    Deleted User

    Guest

    Try StartCoroutine(GravBar());
     
  8. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public static int lives;
    7.     public static bool doubleScore;
    8.     public static bool gravBar;
    9.     public GameObject gravBarObject;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         gravBarObject.SetActive (false);
    14.  
    15.  
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.     if (lives > 3 ) {
    21.             lives = 3;
    22.         }
    23.     if (gravBar == true) {
    24.             StartCoroutine(GravBar());
    25.         }
    26.     }
    27.  
    28.     IEnumerator GravBar ()
    29.     {
    30.         Debug.Log ("Started IE");
    31.  
    32.             gravBarObject.SetActive (true);
    33.             yield return new WaitForSeconds (10f);
    34.             gravBarObject.SetActive (false);
    35.         }
    36.  
    37.  
    38. }
    39.  
    This doesn't work, could it be because the gravbar bool is being set to true via another script?
     
  9. Deleted User

    Deleted User

    Guest

    if it's not giving you exceptions, then yes, that would be why.
     
  10. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    So, doing this should fix it, but it doesn't


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public static int lives;
    7.     public static bool doubleScore;
    8.     public static bool gravBar;
    9.     public bool grav2;
    10.     public GameObject gravBarObject;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         gravBarObject.SetActive (false);
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (lives > 3) {
    20.             lives = 3;
    21.         }
    22.         if (gravBar == true) {
    23.             grav2 = true;
    24.         }
    25.         if (grav2 == true) {
    26.             StartCoroutine (GravBar ());
    27.         }
    28.     }
    29.  
    30.     IEnumerator GravBar ()
    31.     {
    32.         Debug.Log ("Started IE");
    33.  
    34.             gravBarObject.SetActive (true);
    35.             yield return new WaitForSeconds (10f);
    36.             gravBarObject.SetActive (false);
    37.         }
    38.  
    39.  
    40. }
    41.  
     
  11. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    That's because you are starting the coroutine every frame, in which the object would be set to active every frame.
     
  12. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Where should I call StartCoroutine(GravBar ());
     
  13. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    When you actually want to start it. In your case, I suppose when the gravbar is set to true. What you can do is
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class GameManager : MonoBehaviour {
    4.     public static int lives;
    5.     public static bool doubleScore;
    6.     public static bool gravBar
    7.     {
    8.          get { return _gravBar; }
    9.          set
    10.          {
    11.                if(_gravBar != value)
    12.                {
    13.                      _gravBar = value;
    14.                     if(_gravBar)
    15.                        StartCoroutine(GravBar());
    16.                }
    17.           }
    18.     }
    19.     private static bool _gravBar = false;
    20.  
    21.     public GameObject gravBarObject;
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.         gravBarObject.SetActive (false);
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.     if (lives > 3 ) {
    32.             lives = 3;
    33.         }
    34.     }
    35.     IEnumerator GravBar ()
    36.     {
    37.             gravBarObject.SetActive (true);
    38.             yield return new WaitForSeconds (10f);
    39.             gravBarObject.SetActive (false);
    40.     }
    41. }
    42.