YIeld waitforseconds failing for me

ok am very new to this, so sorry for posting (and hope this is the right place) but have also spent ages looking for answers and nothing springs to mind.

Here’s my code…

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Menu : MonoBehaviour
{
    public Image im;
    public bool faded = false;

    void Awake()
    {
    }

    void Update()
    {
        if (faded == false)
        {
            faded = true;
            print("Entering Co-Routine");
            StartCoroutine(MyTest());
        }
    }

    public void PlayButton()
    {
        SceneManager.LoadScene("E1M1");
    }

        IEnumerator MyTest()
    {
        print("Got Into Co-Routine");
        yield return new WaitForSeconds(5);
        im.enabled = false;
        print("Exiting Co-Routine");
    }
}

I have no errors, and when it runs I get the first two print messages (“Entering Coroutine” and “Got Into Co-Routine”, but the image never disables and the “Exiting Co-routine” message never shows.

I am sure I am doing something really stupid and basic, but would appreciate the help because it seems so basic and I have done this before no issues.

The script is attatched to a game object on a UI canvas (not that of the image being disabled).

In case it is of any use… I tried Invoke and that didn’t work either

This is my code for the Invoke version…

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Menu : MonoBehaviour
{
    public Image im;
    public bool faded = false;

    void Awake()
    {
    }

    void Update()
    {
        if (faded == false)
        {
            faded = true;
            print("Entering Invoke");
            Invoke("MyTest", 4);
        }
    }

    public void PlayButton()
    {
        SceneManager.LoadScene("E1M1");
    }

        void MyTest()
    {
        print("Got Into Invoke");
        im.enabled = false;
        print("Completing Invoke");
    }
}

This version again has no errors and prints the “Entering Invoke” message but then stops.

Regards

Moria

Make sure you are not disabling the gameobject that your Menu is on, or otherwise destroying it, or even just disabling the Menu script itself.

Are you also sure you are not seeing any red errors in the console log? Make sure the red button in the upper right corner of the console window is selected to see errors.

Also make sure you don’t have Time.timeScale set to 0.

I had a similar issue, what I did was to put everything on the Coroutine (except the yields) inside a function and call that function on the Coroutine. Don’t know why but it worked for me.

Hope it works :smile:.

And that was the lightbulb. Thank you. I didn’t even know about that setting, but went looking for it and found one of the packages that I imported set the Time.timeScale to 0 for some reason.

I set it back to 1 and everything worked as excpected.

Many thanks for your insight.

Regards

M