Search Unity

Time.TimeScale isn't working

Discussion in 'Getting Started' started by LeeDenbigh, Jan 20, 2015.

  1. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    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?
     
  2. davem250

    davem250

    Joined:
    May 28, 2013
    Posts:
    186
    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 :)
    Cheers.
     
    Last edited: Jan 21, 2015
  3. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    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:

    Code (CSharp):
    1. public void pauseGame(){
    2.         TogglePauseGame();
    3.         if (isPaused)
    4.         {
    5.             Time.timeScale = 0f; // Sets the game time to zero
    6.             PausedMenu.SetActive(true); //Sets the pausedPanel to visible.
    7.  
    8.             Controls.SetActive(false); // Sets the controls to invisible.
    9.         }
    10.         else if (!isPaused)
    11.         {
    12.             Time.timeScale = 1f; // Sets the gameplay speed to unpaused.
    13.             PausedMenu.SetActive(false); // Sets the pausedPanel to invisible.
    14.  
    15.             Controls.SetActive(true); // Sets the touch controls to visible.
    16.         }
    17.     }
    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!
     
  4. davem250

    davem250

    Joined:
    May 28, 2013
    Posts:
    186
    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 :) it never Hurts to try it out :D
     
  5. Oatz

    Oatz

    Joined:
    Oct 21, 2013
    Posts:
    11
    Same problem here..My GUI button function is working, just not pausing the game.
     
  6. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    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.