- Home /
Pause the game
So, I use Time.timescale = 0 to pause my game. When I press the pause button, my game stops. Then when I press it again, the game just starts immediately. I want somekind of a 3 seconds pause time between the paused state and the game state. How should I do that? Here's my code:
public GameObject pauseCanvas;
public void PauseTheScene ()
{
if (Time.timeScale != 0)
{
Time.timeScale = 0;
pauseCanvas.SetActive (true);
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1;
pauseCanvas.SetActive (false);
}
}
}
but my time.timescale is 0, so the delay seconds doesn't work right?
Answer by VisalDXP · Nov 23, 2014 at 10:38 AM
Edited for Beter Understanding:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour
{
private IEnumerator _unscaledTimeRoutine;
public GameObject pauseCanvas;
public void PauseTheScene ()
{
if (Time.timeScale != 0)
{
Time.timeScale = 0;
pauseCanvas.SetActive (true);
}
else if (Time.timeScale == 0)
{
pauseCanvas.SetActive (false);
_unscaledTimeRoutine = DelayResume(3f);
}
}
//Custom routine
IEnumerator DelayResume(float sec)
{
//set t equal to duration that will delayed
float t = sec;
//Keep count down until t <= 0
while (t > 0)
{
t -= Time.unscaledDeltaTime;
yield return null;
}
//when the count down time is out resume game
Time.timeScale = 1;
yield return 0;
}
void Update()
{
if(_unscaledTimeRoutine != null && !_unscaledTimeRoutine.MoveNext())
_unscaledTimeRoutine = null;
//The code above can be break down for better understanding
/*
//Do we have any routine to run ?
if(_unscaledTimeRoutine != null)
{
//if there is then run the routine and check if it is ended ?
bool routineEnded = !_unscaledTimeRoutine.MoveNext();
//if the routine ended then set _unscaledTimeRoutine to null
if(routineEnded)
_unscaledTimeRoutine = null;
*/
}
}
thx your code is working. Would u $$anonymous$$d explaining the code???
Also, another occurred. I can't use animations during that 3 seconds.
if you're using animator controller, then change the Update $$anonymous$$ode to Unscaled Time. If you choose Update $$anonymous$$ode = Normal then it won't update the animation when the Time.timeScale = 0
Normally when the Time.timeScale = 0
, StartCouroutine()
and Invoke()
won't work but the Update
function is still being called normally. So in the code that I wrote above, I declared IEnumerator _unscaledTimeRoutine
for holding our own custom routine then on Update
function I check if _unscaledTimeRoutine
is not null (if it is null then it means that there is no routine to run) then call $$anonymous$$oveNext()
method of _unscaledTimeRoutine
which is return Boolean that deter$$anonymous$$e it could move to next element or not (if it is return false then it means that our routine is ended). So for short, the code in the Update
just use to check does it has any routine to run ? if yes then run it and check, has the routine ended yet ? if yes set it to null else keep running.
and one more thing use Time.unscaledDeltaTime
because when Time.timeScale = 0
it will make Time.deltaTime = 0
as well.
Sorry for my bad English :) Hope you understand.
I edited my code and add some comments to it. hope it help ;)
Answer by Jignesh G. · Nov 23, 2014 at 06:17 PM
OnUpdate will continue to execute even if the timescale is zero.
You can add Add Some custom code inside Update() may beto resolve the issue. Hope this will help you.
Ulternatively use iTween With ignoretimescale and pass "comcomplete" method which might execute even if TimeScale is Zero. (This is not tested, but hopefully this will work).
Answer by Kiwasi · Nov 23, 2014 at 10:36 AM
Implement a custom timer based on one of the unscaled values from the time class.
Answer by idurvesh · Nov 24, 2014 at 08:02 AM
Here is what I done in my game,
OnResumeClicked(){
//calls coroutine when user clicks on resume button
StartCoroutine(resumeTimer());
}
IEnumerator resumeCor(){
float remainTime= Time.realtimeSinceStartup + 3;
while(remainTime > Time.realtimeSinceStartup){
//shows 3, 2, 1 on UI
resumeTimerTextButton.text = "" + (int) ((remainTime+1) - Time.realtimeSinceStartup);
yield return null;
}
Time.timeScale = 1;
}
Hope it helps.
Your answer
Follow this Question
Related Questions
Coroutines, WaitForSeconds with function Update? 3 Answers
Main Menu in different scene - pause game 3 Answers
Pause at end of game 2 Answers
Pause Menu problem 0 Answers