Time.TimeScale isn't working

Hi guys.

I just can’t figure out why Time.TimeScale isn’t working for me. I have watched loads of tutorials, but nothing is pausing my game.

public bool isPaused = false;

void Update() {

if (isPaused)
{
Time.timeScale = 0f;
Debug.Log(“Time 0”);
}
else
{
Time.timeScale = 1f;
Debug.Log(“Time 1”);
}
}
}

public void TogglePauseGame()
{
if (isPaused)
{
isPaused = false;
}
else
{
isPaused = true;
}
}

When I fire the TogglePauseGame function, the debug.log works fine… but the timeScale just isn’t working for me… Does anyone know what I might be missing here?

I am 99.999% sure that your issue lies within the fact that you haven’t been calling the bool isPaused, to set it to true.

example :

public bool isPaused = false;

void Update() {

if(Input.GetButtonDown(“Pause”))
{
isPaused = !isPaused;

if (isPaused)
{
Time.timeScale = 0f;
Debug.Log(“Time 0”);
}
else
{
Time.timeScale = 1f;
Debug.Log(“Time 1”);
}
}
}
}

i am just adding two lines of code to make this code Work and i use it myself so it should Work for you also :slight_smile:
Cheers.

Cheers for the reply.

I am using the new UI. I have a UI pause button that is calling the function onClick. I have change the code a bit:

public void pauseGame(){
        TogglePauseGame();
        if (isPaused)
        {
            Time.timeScale = 0f; // Sets the game time to zero
            PausedMenu.SetActive(true); //Sets the pausedPanel to visible.

            Controls.SetActive(false); // Sets the controls to invisible.
        }
        else if (!isPaused)
        {
            Time.timeScale = 1f; // Sets the gameplay speed to unpaused.
            PausedMenu.SetActive(false); // Sets the pausedPanel to invisible.

            Controls.SetActive(true); // Sets the touch controls to visible.
        }
    }

When I tap the UI Pause button it is calling the pauseGame() function (that way it is changing the bool each time it’s clicked) . But for some reason or other it still isn’t pausing the game, but it is showing the pause menu, so I know that the function is working…

I have a game over screen that is fired with an OnTriggerEnter2D, which I am using the exact same code as above on and the Time.TmeScale works fine with that. I’m wondering if it has something to do with the UI?

It has seriously baffled me!

I have heard some while ago that you cannot set the timescale value to a complete 0! i don’t know if that is what’s causing your problems??? maybe try to set the timescale value to 0.00001f or even lower if possible :slight_smile: it never Hurts to try it out :smile:

Same problem here…My GUI button function is working, just not pausing the game.

Did you fix it Oatz? I figured out what the problem was. I think it’s something to do with the time.timescale being called in the update method. That’s what my problem was anyway.