Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by BilboStabbins · Apr 04, 2013 at 04:54 AM · pausetimescalecountdownresume

Best practice for countdown timer on game resume

Hi all,

I'm creating a coroutine that counts down when the game is resumed from being paused (as seen in Temple Run, Subway Surfer, etc.) I have tried to use Time.realtimeSinceStartup as my timer to display the text after a brief delay, but its not working correctly.

On the first pause-resume cycle the text begins from 2 (instead of 3) then after a second or so 1, then it sets the timescale to 1 as it should. On subsequent cycles, no text is displayed at all, but it is entering the PlayCountdownTimer coroutine as I've checked this. I'm not sure what the problem is so was wondering if anyone can lend a hand with how to get it working. I should also mention that I'm using EZGUI and the SpriteText script for the text.

Also, I'd also be very interested in finding out what the best practice is for this kind of routine. I've seen suggestions of setting timescale to a very small number so that the WaitForSeconds yield instruction can be used, but I'm not too sure how to go about it this way.

Thanks

    void OnPauseGame()
     {
         if (Time.timeScale == 1)
         {
             Time.timeScale = 0;
         }
         else
         {
             StartCoroutine(PlayCountdownTimer(4));
 
             Time.timeScale = 1;
         }
     }
 
     IEnumerator PlayCountdownTimer(int pauseDelay)
     {
         float pauseEndTime = Time.realtimeSinceStartup + pauseDelay;
 
         while (Time.realtimeSinceStartup < pauseEndTime)
         {
             if (Time.realtimeSinceStartup > 0.5f && Time.realtimeSinceStartup <= 1)
                 // Display 3
                 ResumeGameSpriteTextScript_ref.Text = "3";
 
             else if (Time.realtimeSinceStartup > 2 && Time.realtimeSinceStartup <= 3)
                 // Display 2
                 ResumeGameSpriteTextScript_ref.Text = "2";
 
 
             else if (Time.realtimeSinceStartup > 3 && Time.realtimeSinceStartup <= 4)
                 // Display 2
                 ResumeGameSpriteTextScript_ref.Text = "1";
 
             else
                 ResumeGameSpriteTextScript_ref.Text = "";
 
             yield return null;
         }
     }
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 Gurc · Apr 04, 2013 at 05:25 PM

Why do you compare time to a constant? "Time.realtimeSinceStartup > 0.5f"

You should store time in OnPauseGame() like:

float time_started; OnPauseGame() { time_started=Time.realtimeSinceStartup; }

and check the time in coroutine like: if(Time.realtimeSinceStartup > 0.5f+time_started)

I am not so sure for below part: You should "yield return StartCoroutine() " for waiting of execution of countdown menu.

One more thing. What does your code show between seconds "1 and 2" which is not included in if/else conditions?

Comment
Add comment · 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

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't set Time.timeScale back to 1. 2 Answers

Pause, Unpause 1 Answer

How to pause game when incoming call or home button pressed occur 1 Answer

2D main menu as a different scene - pausing and resuming 0 Answers

Pause and resume coroutine 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