How to implement pausing of WaitForSeconds based coroutine?
Greetings,
I am looking for a pausing solution. The issue is that my game has a double score power-up for 10 seconds: private bool isDoubleScoreActive; private int runningDoubleScoreCoroutinesCount;
 EventManager.TriggerEvent("DoubleScore"); // triggers the event
 
 private void OnDoubleScore(object sender, object parameters)
     {
         StartCoroutine(DoubleScore()); // starts the coroutine
     }
 
     private IEnumerator DoubleScore()
     {
         if (!isDoubleScoreActive)
         {
             doubleScoreIndicator.SetActive(true);
             isDoubleScoreActive = true; // any score added gets doubled
         }
         runningDoubleScoreCoroutinesCount += 1; // used to track if we have active coroutines so we can extend the length of the power-up
         yield return new WaitForSeconds(10); // the power-up lasts for 10 sec
         runningDoubleScoreCoroutinesCount -= 1; // the coroutine is done
         if (runningDoubleScoreCoroutinesCount == 0) // if this is the only coroutine, the power-up has ended
         {
             doubleScoreIndicator.SetActive(false);
             isDoubleScoreActive = false;
         }
     }
 
               And this kind of pattern makes it impossible to pause the game, anyone has any idea how to solve this? The only idea I have is to make a function update time each frame and stop changing time when paused is set to true. Are there any better solutions?
Thanks for your time
Answer by krisuman · Apr 25, 2017 at 04:02 PM
WaitForSeconds is using scaled time, so when you set your timescale to 0 your corutine will not be executed in pause. Or you want to run this corurine even when in pause mode? In that case use lWaitForSecondsRealtime which is independet from timescale.
I didn't know about timescale, thanks for pointing it out!
Your answer
 
             Follow this Question
Related Questions
Enable/disable is not working as expected 2 Answers
How to reduce lag in coroutines - specifically WaitForSeconds()? 1 Answer
How to pause my game when using Time.DeltaTime? 0 Answers
Could not load source 'Coroutines.cs': No source available. 1 Answer
Am I using this Coroutine and IEnumerator correctly? 1 Answer