- Home /
Unable to change Time.timeScale
I was trying to make a pause menu, which seemed simple enough. However, nomatter what I change Time.timeScale to it instantly reverts it back to 1.. after a while I tried manually changing it under Edit -> Project Settings -> Time, but it automatically reverts it back to 1 there too..
Note: I loaded the script into a different project and it worked perfectly. So it may be an issue with my project settings? They're all just the defaults as far as I'm aware though..
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
bool paused = false;
void Update ()
{
if ( Input.GetKeyDown( KeyCode.Escape ) )
{
Pause();
}
Debug.Log ( Time.timeScale );
}
void Pause()
{
paused = !paused;
Time.timeScale = 1.0f - Time.timeScale;
}
void OnGUI()
{
if ( paused )
{
var rect = new Rect( 10, 10, 100, 30 );
rect.center = new Vector2( Screen.width/2, Screen.height/2 - 35 );
if ( GUI.Button( rect, "Return to Game" ) )
{
Pause();
}
rect.center = new Vector2( rect.center.x, rect.center.y + 35 );
if ( GUI.Button( rect, "Quit to Main Menu" ) )
{
// unpause / reset timescale before loading new scene
Pause();
Application.LoadLevel( "MainMenu" );
}
}
}
}
Answer by ahmedbenlakhdhar · Dec 08, 2014 at 12:33 AM
You may be changing Time.timeScale
value in another script.
Yeah, after singling out which script it was I remembered something I changed late last night.. I was changing timescale in an 'else' block that ran every frame (like a dumbass)
Answer by static_cast · Dec 08, 2014 at 12:04 AM
Time.timeScale = 1f - Time.timeScale;
In your script, you're missing a semicolon after your line. It needs to be there.
Also:
You can stop the execution of FixedUpdate() with Time.timescale=0
You cannot stop the execution of Update()/LateUpdate(), unless you disable the script.
Ah, woops. I fixed that though, and the issue persists
You can stop the execution of FixedUpdate() with Time.timescale=0
You cannot stop the execution of Update()/LateUpdate(), unless you disable the script.
Typically if you want to pause your game, you just want to disable input and enemy scripts [only in singleplayer]. You could just use if(isPaused())
and have the isPaused() function check some sort of global variable for each script that needs to be paused.
In my game, if you go to the menu, I use if(Screen.lockCursor)
.
Ah, that's a good idea as well. Thanks did get it figured out though, ahmedbenlakdhar's suggestion re$$anonymous$$ded me of something I changed last night that was causing the issue..
Well, just to be clear, you will probably have problems with this pause script in the future.
Your answer
Follow this Question
Related Questions
How do I stop 'timeSinceLevelLoad' without pausing entire game? 2 Answers
2nd independent deltaTime and timeScale variables, or a way to mimic this? 1 Answer
Is it possible to change Time.timeScale to a value bigger than 100? 0 Answers
Need help affecting 2d jump with UnscaledDeltatime 0 Answers
Pause menu doesn't pause everything 0 Answers