- Home /
How to pause a Coroutine?
I am using Coroutines which yield, WaitForSeconds method. I want to implement a Pause system which pauses these coroutines too.
That is if a Coroutine continues every 5 seconds. And if I pause that coroutine at 3 seconds. I need it to continue after 2 seconds after I unpause.
Why do I want this?
I don't want to set timeScale to 0 because I want animations while game is paused.
By using Pause boolean in a if condition, I can pause only those coroutines which run every frame.
I can achieve this functionality by using Update() method. But then there will be no point in using coroutines at all.
@digzou This is what I wanted. Thanks :)
I guess I asked the wrong question for my Problem.
Answer by digzou · May 27, 2015 at 07:32 AM
If you want to use Animations when timescale is 0, U can use the AnimatorUpdateMode.UnscaledTime property. And use timescale = 0 to pause the game.
http://docs.unity3d.com/ScriptReference/AnimatorUpdateMode.UnscaledTime.html
Answer by f4bo · May 27, 2015 at 07:40 AM
how about to put this in your coroutine?
for (int i = 0; i < 5; i++) {
while (paused) {
yield return new WaitForSeconds (0.2f);
}
yield return new WaitForSeconds (1f);
}
hi @f4bo. Thnx for the answer.
This can be a cool method to pause any particular coroutine (with yield WaitForSeconds), with an Offset of 1 second only.
But it also runs every second ins$$anonymous$$d of 5 seconds. And when paused every .2 seconds
.1 - .3 seconds is the industry standard, as any longer will have a visibly noticeable delay before actions continue. This will often make your game appear to be lagging. Keep it low.
Answer by ShabihDesperado · May 27, 2015 at 07:42 AM
While(timeCount < 5)
{
if(!isPaused)
timeCount += Time.Deltatime
yield return null;
}
@ShabihDesperado This can be a solution to pause a coroutine. But it pauses only those coroutines which run every frame.
All coroutines run every frame. Unless they are not active, and then they are no longer relevant to this post.
But you said that you didn't want to touch timeScale, so the only solution (with your restrictions) is this. The best solution is the one of @digzou but is not what you asked neither.
As i stated in point no. 2 -
By using Pause boolean in a if condition, I can pause only those coroutines which run every frame.
I wanted something like: When pausing -
Stop the coroutine.
save its state. For example:- if WaitForSeconds(5). And i stop the coroutine at 3 seconds. save state like 2 seconds remaining.
And when unpausing -
Start the coroutine from that saved state. Thats is resume after 2 seconds.
These things are not possible without a timer. But Unity might be using something internally to resume an yielded coroutine.
Well anyways thnx for the answer :)
Answer by Chambers88 · Jan 24, 2019 at 02:53 PM
Unfortunately there's no easy way to do it...
There seem to be some assets that solve your problem, though I haven't tested them out myself: https://assetstore.unity.com/packages/tools/safe-coroutine-17316
I personally stopped using coroutines for things like enemy behaviors that needed to be paused/resumed in my game :(
Use Update() instead and enable/disable the script. Depending on what you're doing, a state machine might work to simulate the steps of your coroutine inside the update.
Answer by eomerb · Apr 15, 2020 at 08:29 AM
hello there I know its old topic but while I was searching for an answer I come up this idea what I want to do is display an image for given seconds
void playImage()
{
WWW www = new WWW(url);
image.texture = www.texture;
totalWaitTime = duration;
currentWaitTime = 0;
StartCoroutine(wait());
}
IEnumerator wait()
{
if (currentWaitTime < totalWaitTime && !isPaused)
{
yield return new WaitForSeconds(1);
currentWaitTime++;
StartCoroutine(wait());
}
else if (currentWaitTime <= totalWaitTime && isPaused)
{
}
else
{
//continue
}
}
instead of waitforseconds(duration) I wait for 1 second and check if not paused call StartCoroutine(wait()); again and check for the next second, if it is paused do nothing and when continue is pressed or triggered I call StartCoroutine(wait()); again instead of whole second u can divide it to smaller times like 0.1f its up to you I find it pretty easy and hope it will help others