- Home /
Pause a coroutine not working
Hi - I'm struggling to get a coroutine working correctly.
Whilst a player is inside a collider, I set a coroutine to active. However, if the player is set to inactive whilst inside the collider, I want this coroutine to pause where it is in the sequence, and once this player is set to active again, I want the coroutine to resume where it is in the sequence.
Does anyone know what I'm doing wrong?
#pragma strict
var player : GameObject = null;
var cameraPortacabin : GameObject;
var text44 : GameObject;
var text45 : GameObject;
var text46 : GameObject;
var screen1 : GameObject;
var lastKnownState : boolean = true; // set default to false if camera starts disabled
function Start () {
text44.SetActive(false);
text45.SetActive(false);
text46.SetActive(false);
screen1.SetActive(false);
}
function Update ()
{
if (cameraPortacabin.active != lastKnownState)
{
lastKnownState = cameraPortacabin.active;
if (lastKnownState) // same as if (lastKnownState == true)
portacabinSequence();
}
if (cameraPortacabin.active == false || player.active == false) {
StopCoroutine("portacabinSequence");
text46.SetActive(false);
screen1.SetActive(false);
}
}
function portacabinSequence () {
screen1.SetActive(true);
text44.SetActive(true);
yield WaitForSeconds (9.5);
text44.SetActive(false);
text45.SetActive(true);
yield WaitForSeconds (11);
text45.SetActive(false);
text46.SetActive(true);
}
Best, Laurien
Answer by redeemer · Jun 01, 2015 at 10:51 PM
Hi,
If you don't have any other coroutines you need to keep active, you could try with StopAllCoroutines.
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopAllCoroutines.html
But I want to pause it rather than stopping it altogether - or have I understood that wrong?
Once the player is active again, I'd like the coroutine to start from the point where it was paused until reaching the end of the coroutine.
Well, maybe I'm wrong but I think you didn't undertand how a coroutine behaves. Your coroutine would be trigger each time last$$anonymous$$nownState equals true. Your coroutine does not have a loop, so it would be executed only once (each time is called, that mean if you called it 20 times it would be executed 20 times, one per call) with a total execution time of 20.f secs. It's possible that when you try to Stop your coroutine when cameraPortacabin or player are not active, your coroutine had already finished or maybe it was not even started in the first place. If you want to implement a "Pause" system, this is not the correct way. If this is the case, ther're other answers where this topic had already been discussed. As an example, this link could be of interest, it's a free example available at the Unity Store : https://www.assetstore.unity3d.com/en/#!/content/1020
Is this other answer you have multiple possible solutions for pausing your game :
http://answers.unity3d.com/questions/7544/how-do-i-pause-my-game.html
If your goal is not to make a pause system I'm sorry in advance and could you please be more specific about what is you want to accomplish?
Sorry for not being clear!
Yes - the coroutine is only executed once (each time the player enters a trigger).
So essentially I need:
if (player.active == false) {
//remember what point in the coroutine this is and stop coroutine
if (player.active == true) {
Start coroutine at the last point the coroutine was in the sequence until reaching the end of the loop.
But I can't work out how to do this? (I haven't formatted the code correctly as I'm a bit embarrassed as it's not really code - I just can't work out how to go about this)
Ok, but when you say "Start coroutine at the last point the coroutine was in the sequence until reaching the end of the loop." to which loop are you refering, because in your coroutine there is no loop, and I don't think is possible to stop at a point in the code to jump back at it (goto died a long time ago XD)...
In order for a coroutine to have a loop, you must declare a for or while loop such as :
function portacabinSequence () {
while(player.active) {
screen1.SetActive(true);
text44.SetActive(true);
yield WaitForSeconds (9.5);
text44.SetActive(false);
text45.SetActive(true);
yield WaitForSeconds (11);
text45.SetActive(false);
text46.SetActive(true);
}
}
For what you want to implement I would recommend to create different possible states in which the player can be left and the logics for each one of them.
I also think that even if you call StopCoroutine("CoroutineName") or StopAllCoroutines, it will execute the cicle it's in until the end and then it'll exit, so you can't stop it at a specific point.
Without further explanation of your code, scene or goal I really can't say anything else, sorry...
Your answer
![](https://koobas.hobune.stream/wayback/20220613200534im_/https://answers.unity.com/themes/thub/images/avi.jpg)