- Home /
Nothing happening after WaitForSeconds C#
Hi I'm having some problems with using WaitForSeconds for a menu system im writing, however for some reason after calling WaitForSeconds the compile afterwards doesn't compile.
IEnumerator Test()
{
yield return new WaitForSeconds(1);
go = true;
}
For some reason the "go = true;" line never gets called and I've tried using Debug.Log to get some info out of it but the code simply wont run after the yield.
Also I'm having problem with WaitForSeconds breaking while loops.
while (true) { if (Input.GetAxisRaw("LeftVertical" + 1) < -0.9f) { Debug.Log("up"); selectedIndex = menuSelection(menuOptions, selectedIndex, "up"); yield return new WaitForSeconds(delay); Debug.Log("delay ended?"); } else if (Input.GetAxisRaw("LeftVertical" + 1) > 0.9f) { Debug.Log("down"); selectedIndex = menuSelection(menuOptions, selectedIndex, "down"); yield return new WaitForSeconds(delay); Debug.Log("delay ended?"); }
yield return null;
}
The strangest thing is that the code for the main menu which is almost the same the loop doesn't break, IF you start on that level, as soon as you load it again the loop breaks when it reaches a WaitForSeconds.
Any idea? I bet I missed something silly simple as usual.
Thanks in Advance
//Nick
Answer by Nick 26 · May 02, 2011 at 10:03 AM
It seems that the problem was that I had paused the game by setting the timescale to 0 and that this for some reason cancel out the yield commands. Don't take my word for it though as I haven't taken the time to confirm it.
I solved it by making my own delay using Time.realtimeSinceStartup in the following way
Start()
{
timeStarted = Time.realTimeSinceStartup;
}
and then in update()
if(timeStarted + delay < Time.realtimeSinceStartup)
Answer by Mike 3 · Apr 21, 2011 at 02:31 PM
You have to call your coroutine functions with StartCoroutine(YourCoroutine());
Thanks for the answer, however I'm already doing that so that's not the problem.