- Home /
The question is answered, right answer was accepted
Coroutine yield return new Waitforseconds not working
Here's my code:
if(CubeScript.startSpeed <= 25){
StartCoroutine("IncSpeed");
}
And my coroutine:
IEnumerator IncSpeed(){
Debug.Log("before");
yield return new WaitForSeconds(5);
Debug.Log("after");
}
It debugs "Before" in the console, but not "after". Am I not using this coroutine correctly?
You are calling it correctly. By chance have you tried lowering the wait to 1? Sometimes 5 seconds can seem like and eternity when you're testing.
There are several things that could cause a coroutine to stop. The most common thing that I have seen is the object being destroyed during a coroutine through some other event. Subsequently, the coroutine will be destroyed with the object that was running the coroutine.
Disabling the game object the script is attached to or setting Time.timeScale = 0 will also stop coroutines.
@Zhinkk, feel free to post the solution you found as an answer and mark it as accepted. It is perfectly fine to post and answer to your own question when you discover what the problem was. That way other people with this problem can see what you did to solve it when they run a search.
Answer by Zhinkk · Oct 10, 2013 at 01:42 AM
I failed to realize the script was being destroyed in the process. Thanks, though! I appreciate the help. :)
It's a simple thing to overlook, yet takes awhile to find the fix. Thanks!
Follow this Question
Related Questions
Coroutine "While" Setup 1 Answer
Reset WaitForSeconds Coroutine 1 Answer
Yield Never Works 4 Answers
Is there a yield WaitForSeconds type code for the update? 2 Answers
Please help with While Loops 1 Answer