- Home /
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!
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;
}
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).
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
I have seen a few codes by different people and do I have to call StartCoroutine before using IEnumerator
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.
this is from 2010! and the answer is outdated, check this: http://blogs.unity3d.com/2015/12/01/custom-coroutines/
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;
}
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.
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);
}
}
I think you need to divide by the timescale rather than multiply:
yield WaitForSeconds(pauseDelay / Time.timeScale);
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/
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.
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
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
}
Your answer

Follow this Question
Related Questions
Is this correct to modify real time unit to match with timescale? 2 Answers
Physics + BulletTime = problems 1 Answer
change timescale for a specific gameobject 0 Answers
Level completed but still playable (timescale problem) 0 Answers
How to return unity back to its normal time scale and fixedDeltaTime 0 Answers