- Home /
How to stop co-routine?
When two co-routine is running.Then how to stop first co-routine? GLOBALS.stableTime = 5;
IEnumerator StableWaittingTime ()
{
yield return new WaitForSeconds (1f);
if (GLOBALS.stableTime == 0) {
GameManager.instance.LevelFaildMethod ();
} else {
GameManager.instance.stableWaittingTime.text = GLOBALS.stableTime.ToString ();
GLOBALS.stableTime--;
StartCoroutine ("StableWaittingTime");
}
}
Answer by Kiwasi · Aug 08, 2014 at 08:09 PM
Two methods come to mind
The best method I can suggest is using the string form of StartCoroutine. Then you can call StopCoroutine directly.
StartCoroutine("StableWaittingTime");
StopCoroutine("StableWaittingTime");
The other method is build you coroutine so is automatically aborts when the condition is satisfied.
Thank you Bored$$anonymous$$ormon, but i have one doubt if i write startcoroutine first and i write StopCoroutine second then strarted Coroutine will be stppoed.
$$anonymous$$y issue is i want to stop older one coroutine then i want to start new coroutine cos i handle only one variable in both coroutine so that in this case problem is that when i start second coroutine at that time the var's value is increased speedy (means twise) because that my older one coroutine is already start and the variable is same.
What can i do for prevent this?
I can't believe I'm writing this, but the following code will stop the old coroutine before starting the new one.
StopCoroutine("StableWaittingTime");
StartCoroutine("StableWaittingTime");
How can you know that your co-routine is stopped and can you send me your tested code because that in my script that not worked cos as i m say i have two button for to start the co-routine.you used to buttons for start that co-routines.
The docs are clear enough on how to do this, I've given you the pointers you need to figure it out. Can't help much more. I'm out on this one.
Try stamp collecting ins$$anonymous$$d?
Hey, Bored$$anonymous$$ormon its work thanking to you.
Answer by Andres Barrera · Aug 08, 2014 at 07:40 PM
Take a look at this: https://gist.github.com/onevcat/5261702
It's not my work, but I have tested it and is really helpful.
Answer by Hardik · Nov 18, 2014 at 07:55 AM
You can also use yield break
for stop co-routine
IEnumerator DoCheck() {
yield return new WaitForSeconds (1f);
//Do your stuff here
yield break;
}