- Home /
Time.timescale problems
Hello i have a javascript where i can pause my game but it seems not to work, i can unpause but not pause again. If you can tell what the problem is throught the script then tell me thanks (:
#pragma strict
var isPaused : boolean = false;
function Update ()
{
//Pause
if(Input.GetKeyDown(KeyCode.P) && isPaused == false)
{
isPaused = true;
}
//UnPause
if(Input.GetKeyDown(KeyCode.P) && isPaused == true)
{
isPaused = false;
}
//Pause
if(isPaused == true)
{
Time.timeScale = 0.0;
}
if(isPaused == false)
{
Time.timeScale = 1.0;
}
}
Answer by darthtelle · Aug 31, 2015 at 03:16 PM
Your code is probably getting caught in the unpause check. Because everything in the Update() function happens in the same frame, if you press "P" and then set isPaused to true, when it gets to the next line, "P" is still pressed, and isPaused is now also true, so it sets it to false. You'd be better off putting it in an if statement.
void Update()
{
if(Input.GetKeyDown(KeyCode.P))
{
isPaused = !isPaused;
if(isPaused)
Time.timeScale = 0.0f;
else
Time.timeScale = 1.0f;
}
Thank you sir for your time, and thanks for answearing my question!
Your answer
Follow this Question
Related Questions
How to assign a pre-set position with the Time.time component? 0 Answers
Pausing Object With Time.Timescale excepting line renderer 1 Answer
Time.timeScale seems doesn't work with Update 1 Answer
Problem freezing the game with 1 game over script over many objects 0 Answers
Need help affecting 2d jump with UnscaledDeltatime 0 Answers