Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Raxume · Apr 25, 2017 at 02:52 PM · coroutinepausewaitforsecondspause game

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raxume · Apr 25, 2017 at 04:10 PM 0
Share

I didn't know about timescale, thanks for pointing it out!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges