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
13
Question by BogdanDude · Jan 08, 2010 at 09:57 AM · timescale

Freeze game using Time.timeScale = 0, wait 3 secs and resume play automatically. Is it possible?

I'd like to use time.timescale = 0 to freeze the game, show an image, wait 3 seconds based on a timer, and then resume automatically using time.timescale = 1; I think 'yield return new WaitForSeconds(3F);' doesn't work when time.timescale == 0. Am I wrong? :)

Is there some other way, if I'm right with the above?

Thanks!

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

11 Replies

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

Answer by duck · Jan 08, 2010 at 10:45 AM

You're right, if you set the timescale to zero, the WaitForSeconds command won't work. For example, in the following code, the "WaitForSeconds" actually ends up taking 10 times as long as whatever duration is provided (as the variable 'p') to complete because of the timescale:

private IEnumerator Pause(int p)
{
    Time.timeScale = 0.1f;
    yield return new WaitForSeconds(p);
    Time.timeScale = 1;
}

And with a timescale of 0.0, the WaitForSeconds never completes. The way around this is to time the duration yourself using the Time.realtimeSinceStartup property, which works independently of the timeScale. See the following example:

private IEnumerator Pause(int p)
{
    Time.timeScale = 0.1f;
    float pauseEndTime = Time.realtimeSinceStartup + 1;
    while (Time.realtimeSinceStartup < pauseEndTime)
    {
        yield return 0;
    }
    Time.timeScale = 1;
}
Comment
Add comment · Show 14 · 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 Demigiant · Dec 06, 2011 at 12:26 PM 0
Share

Thanks for the great trick Duck. I was getting crazy finding a way to have a Timer who wasn't influenced by timeScale (also considering the the C# Timer class doesn't work correctly with Unity).

avatar image Rayan Adam · Nov 12, 2012 at 12:58 PM 0
Share

Can you please provide the full script of waiting 3 seconds then resume, because i didn't understand the how it works completely. thanks in advance.

http://answers.unity3d.com/questions/346970/wait-3-seconds-then-resume-c.html

avatar image noobme · May 14, 2013 at 10:01 AM 0
Share

I have seen a few codes by different people and do I have to call StartCoroutine before using IEnumerator

avatar image Chubzdoomer · Oct 26, 2015 at 03:31 AM 0
Share

Why do you list "int p" as an argument/parameter in your second example, yet use it nowhere in the code that follows? That makes no sense to me.

avatar image PshychoRZR · Mar 24, 2016 at 08:16 PM 0
Share

Hey can you tell how to use this snippet!

avatar image Soraphis PshychoRZR · Apr 23, 2016 at 08:26 PM 0
Share

this is from 2010! and the answer is outdated, check this: http://blogs.unity3d.com/2015/12/01/custom-coroutines/

Show more comments
avatar image
12
Best Answer

Answer by Eric5h5 · Jan 15, 2010 at 11:43 AM

A simpler but effective way is just to use a really small value for timeScale. Unless you intend on waiting literally for thousands of years, it's just as good as 0.

function PauseWaitResume (pauseDelay : float) {
    Time.timeScale = .0000001;
    yield WaitForSeconds(pauseDelay * Time.timeScale);
    Time.timeScale = 1.0;
}
Comment
Add comment · Show 5 · 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 b_ill · Sep 11, 2012 at 03:14 AM 0
Share

Thanks, this was exactly the solution I needed in order to call a Delay function from Update to wait a few seconds before transitioning to my GameOver scene.

avatar image luniac · Feb 13, 2015 at 10:04 PM 0
Share

didn't work for me, i set Time.timeScale elsewhere to .0000001 This only seems to work if you change timescale within the coroutine itself

 function Start () {
         OrientationCheck();
 }
 
 function OrientationCheck(){
     while(true){
         Debug.Log("CHEC$$anonymous$$");
         yield WaitForSeconds(1.0f * Time.timeScale);
     }    
 }
avatar image Chubzdoomer · Oct 26, 2015 at 03:29 AM 0
Share

Doesn't work.

avatar image dillon_yeti · Apr 23, 2016 at 08:12 PM 0
Share

I think you need to divide by the timescale rather than multiply:

 yield WaitForSeconds(pauseDelay / Time.timeScale);
avatar image Soraphis dillon_yeti · Apr 23, 2016 at 08:25 PM 0
Share

this is from 2010! and even your answer was to a comment from Oct 2015. and all this answers are outdated, check this: http://blogs.unity3d.com/2015/12/01/custom-coroutines/

avatar image
3

Answer by LeoGurung · Aug 18, 2016 at 01:13 PM

In my opinion the simplest way to do this is by using WaitForEndOfFrame() function in Coroutine and Time.unscaledDeltaTime for timer.

 void PauseAndResume()
 {
          Time.timeScale = 0;

          //Display Image here

          StartCoroutine(ResumeAfterNSeconds(3.0f));
 }
 
 float timer = 0;
 IEnumerator ResumeAfterNSeconds(float timePeriod)
 {
          yield return new WaitForEndOfFrame();
          timer += Time.unscaledDeltaTime;
          if(timer < timePeriod)
                     StartCoroutine(ResumeAfterNSeconds(3.0f));
          else
          {
                     Time.timeScale = 1;                //Resume
                     timer = 0;
          }
 }
 
 

I hope it helps, Cheers.

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
avatar image
2

Answer by theagemaway · Sep 25, 2017 at 04:48 PM

This function probably didn't exist at the time of the OP, but there is a WaitForSecondsRealTime now.

https://docs.unity3d.com/ScriptReference/WaitForSecondsRealtime.html

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
avatar image
2

Answer by Harinezumi · Feb 19, 2018 at 02:43 PM

The answers accepted at the time of writing this reply are from 2010. The Unity API has changed a lot since then, and now Time.unscaledDeltaTime is also available, which makes doing this a lot easier:

 private IEnumerator PauseForSeconds (float pauseDuration) {
     float originalTimeScale = Time.timeScale; // store original time scale in case it was not 1
     Time.timeScale = 0; // pause
     float t = 0;
     while (t < pauseDuration) {
         yield return null; // don't use WaitForSeconds() if Time.timeScale is 0!
         t += Time.unscaledDeltaTime; // returns deltaTime without being multiplied by Time.timeScale
     }
     Time.timeScale = originalTimeScale; // restore time scale from before pause
 }
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
  • 1
  • 2
  • 3
  • ›

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I pause my game? 24 Answers

Time.timescale set to 0 whenever i start 7 Answers

hi i just got the ultimate fps camera and it is not compatible with pausemenus 0 Answers

Lots of !IsFinite() Errors and FPS crawling to a halt - How to debug? 1 Answer

Stop force from being applied during pausescreen 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