- Home /
Coroutine not running after yield return new WaitForSeconds
I have a coroutine that is used to move objects.
IEnumerator Move(float start, float end, float length, float delay)
{
yield return new WaitForSeconds(delay);
for (float i = 0; i < 1; i += 0.01667f*(1/length))
{
MoveEndTapeTex = Mathf.Lerp(start, end, i);
yield return null;
}
MoveEndTapeTex = end;
}
The problem was that the coroutine never passes the yield return new WaitForSeconds(delay); if the delay is greater 0. Does anyone know what the problem is and how to fix it?
Your loop increment doesn't make any real sense, it's just an arbitrary number and therefore won't work. You need to use Time.deltaTime.
Answer by BenKurdziel · Oct 04, 2014 at 05:32 AM
This won't be a very intelligent answer, but if I remember correctly, for loops in IEnumerators are HIGHLY advised against for performance and stability reasons. Also, have you tried putting a Debug.Log("Test"); after the yield to see if anything is called afterwards? It's possible the following code just doesn't function like you'd like it to.
i have tested with the debug.log and it does not run if there is a delay. Is it just for loops that are not advised in IEnumerators because i do not know how to do this task without a type of loop eg while
Thanks
Just to be sure, you are using "StartCoroutine($$anonymous$$ove(params));" and not just "$$anonymous$$ove(params);" correct?
And you can totally use any type of loop in a coroutine. Sometimes it's necessary depending on what you are doing. But there's a little trickiness in managing them because you need to Stop them. They can also be somewhat processor intensive depending on how many you are using and how much they are doing each time.
I am using startcoroutine Thanks for the info about loops :)
Just a thought: WaitForSeconds will not return when timescale =0.
What? There are no issues at all with loops in coroutines. That doesn't even begin to make sense, sorry.
Answer by akeplinger · Aug 07, 2015 at 02:27 PM
smallbit has the solution in my case. One developer started using timescale=0 when a game is paused. We were using a coroutine to handle fading up a loading screen and it was never getting past the first yield and this was why. When Time.timescale=0 WaitforFixedUpdates don't happen and waitforseconds never returns. Just setting timescale to 1 at the start of the function fixed the issues.
Answer by fonko · Nov 06, 2015 at 01:44 PM
in my case it wasn't the timescale or the object being destroyed.. (its my game manager so its live during all the game) but i forgot that i used StopAllCoroutines(); around my code and it was killing this particular coroutine with other routines that actually needed to be killed... so now i need to figure out how to let this particular coroutine alive and kill the others.
check if you have a StopAllCoroutines(); around your code.
Your answer
Follow this Question
Related Questions
Yield WaitForSeconds doesn't work, give syntax error 1 Answer
yield WaitForSeconds compatibility? 0 Answers
Create a 10 second delay using WaitForSeconds 2 Answers
Yield only working once? 0 Answers
Wierd issue with Coroutines? 2 Answers