Other
why does timescale not work?
I'm making a game with a pause menu, so i use timescale= 0; to pause the game, but it doesn't work. how should i do this? using UnityEngine; using System.Collections;
public class PauseMenu : MonoBehaviour {
public GameObject PauseUI;
private bool paused = false;
void Start(){
PauseUI.SetActive(false);
Time.timeScale = 1;
}
void Update(){
if (Input.GetKeyDown(KeyCode.P)){
paused = !paused;
}
if(paused){
PauseUI.SetActive(true);
Time.timeScale = 0;
}
if(!paused){
PauseUI.SetActive(false);
Time.timeScale = 1;
}
}
}
"Doesn't work" is not useful information. What exactly isn't working?
when i play the game in unity and I press "p" it should "freeze/pause" the game but it isn't pausing.
what do you mean with "the game isn't pausing"? The only thing Time.timescale affects is the Time.deltaTime Variable. SoundSources, Animations, etc. will still play. You have to slow / stop them on your own.
Does the paused
variable get set when P is pressed? Is timeScale
actually getting set to 0? You can use Debug.Log to log messages to the console to display the values of variables and help you trace through what the program is doing. You can also use the debugger to set breakpoints and trace through code execution.
I've used the Debug.Log function and it's working, but the game wont pause
with the Debug.Log(Time.timescale) the console says that the timescale = 0; but the game isn't paused
Are you using Time.deltaTime
in your movement calculations? I'm pretty sure you need to be using frame independent movement in order for timeScale
to work propertly.
I've a timer with Time.deltaTime but in the script for the player there is nog Time.deltaTime
Well, unless you are using Time.deltaTime in your calculations then timeScale will have no effect at all beyond not running FixedUpdate. All it does is scale deltaTime by multiplying the actual deltaTime by timeScale. But you have to be using deltaTime in your movement functions or there will be no effect.
As the discussion is already going in right direction. But I wanna add one more thing in this.
Pls open the window of 'Time' from Edit>Project Setting>Time. and check whether the TimeScale is switching his value .
And do manually set the value of TimeScale .. if on TimeScale 0 the game freeze then everything is fine expect your given code. One more thing the Animations will not Stop on TimeScale 0
The problem is that TimeScale doesn't switch to 0 in the Time$$anonymous$$anager
As described above, add Debug.Log(Time.TimeScale);
to your Update(). $$anonymous$$aybe any other script is setting the TimeScale back?
Answer by SuperEpicDubstepGlassesOfPower · Dec 01, 2015 at 06:23 PM
Here's The code for a pause menu in a game I'm making. Some of it is similar to your code and so far it is working. You could use parts of it (the time scale part) It might fix it. I have used an object called 'PauseMenuHolder' to have the UI on.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PauseScript : MonoBehaviour
{
GameObject PauseMenuHolder;
bool paused;
void Start()
{
paused = false;
PauseMenuHolder = GameObject.Find("PauseMenuHolder");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
paused = !paused;
}
if (paused)
{
PauseMenuHolder.SetActive(true);
Time.timeScale = 0;
}
else if (!paused)
{
PauseMenuHolder.SetActive(false);
Time.timeScale = 1;
}
}
public void Resume()
{
paused = false;
}
public void MainMenu()
{
Application.LoadLevel(1);
}
public void Quit()
{
Application.Quit();
}
}
Hope I helped in some way :P
Thanks, but the problem is still there. In the background you can move the ball, you can just play further.
$$anonymous$$aybe it could be that you are controlling the ball with translation. I don't think translating and object is affected by timescale. It is mostly just physics that relies on time. (I think)
Doesn't mean you can't use translation though.
Answer by jurgendekker · Dec 01, 2015 at 08:48 PM
@SuperEpicDubstepGlassesOfPower Thanks, but the problem is still there. In the background you can move the ball, you can just play further.
since google send me out here, I will post how I solved some timescale problem of $$anonymous$$e. Hope that helps. I had a problem with time.timescale. $$anonymous$$y pause screen was alright but then it stopped working. It happened to be that I imported some asset with scripts and these scripts have Time.timeScale set to 1 on Start(). Just fixed it. If anyone having the same problem, just look through your new assets scripts and try to find Time.timeScale there. Bye.
Follow this Question
Related Questions
why doesn't timescale work? 0 Answers
why the Timer isn't working 0 Answers
Wait time after coroutine's wait seconds is complete 0 Answers
Highscore - BestTime 1 Answer
How to use time, when timeScale is 0? 2 Answers