unity 3d student

Beginner B27 – Using Time.timeScale to Pause

How to Pause the game by implementing Time.timeScale.

Code Used (Javascript)

  1. var paused : boolean = false;
  3. function Update () {
  4.  if(Input.GetButtonUp("Jump")){
  5.   if(!paused){
  6.    Time.timeScale = 0;
  7.    paused=true;
  8.   }else{
  9.    Time.timeScale = 1;
  10.    paused=false;
  11.   }
  12.  }
  13. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites


    what is the game object called manager?

    • Bruno – its just an empty object to host scripts. I have placed this in because I wanted to show this particular functionality unattached to any functional object in the scene – in case viewers think that the script needs to be applied to a particular object to work. I’ve named it manager for no other reason that it might play host to scripts that manage what is happening in the game – for example I might store scores on a manager object – but I could name it anything.

    Is it possible that when paused, the camera rotates around say the character in a 3rd or even 1st person game? Or does this still require the time to progress? Which does not progress when paused obviously.

  • var paused : boolean = false;

    function Update () {
    paused = !paused;
    Time.timeScale = 0;
    Time.timeScale = 1;

    would be I think easier to understand since right now, because of this (!paused) it looks like the pause works when timeScale = 1. Also – if the game is not using any slow motion effects based on timeScale (and I guess that for 95% of games it’s true) we could throw that paused variable and just check Time.timeScale itself (which is available globally in every object):

    function Update () {
    Time.timeScale = (Time.timeScale == 0 ? 1 : 0);

    @Ron – I’ve never tested but since it calls Update functions with timeScale = 0 you should be able to rotate your camera “manually” using some Update function and it should work.

    • Pendowski, you make excellent points here, thank you. What I might do is make a post about differing degrees of script simplification where I show something like this as it’s clear there is a lot of possibility to demonstrate more or less efficient ways of scripting.

    Thank you very much. This tutorial was very usefull for me. It’s also easy to understand. Can you post a tutorial on creating a GUI pause menu. Thank you in advance. Bye!

    • Benjamin, I am currently deciding how to approach the various aspects of GUI elements but whilst I do you could simply combine this tutorial and the guitexture tutorial on the site, or read the script reference for GUI.Label and start from there.

    The timeScale value has no effect on things that are frame-dependent. However, fixedDeltaTime-dependent (FixedUpdate, fixedDeltaTime, etc.) things will stop.

    The code below would stop:

    function FixedUpdate(){
    timer -= Time.fixedDeltaTime


    function Update(){
    timer -= Time.deltaTime

    would continue counting down. Logical really, if Update() stopped when Time.timeScale = 0, you would not be able to resume the game. Just thought people would want to know.

    • Very good point indeed – I hadn’t considered timers in this context, this of course is why is crucial to have a paused boolean as you can use it for things like this, rather than referencing the current timeScale value.

    I cant wait to see more challenges… i can see some interesting stuff in the future =D

  • Thanks dude, you rock, very easy, very useful.

  • That’s another really excellent lesson on a topic that’s been mildly bugging me for months. I’ll look at the GUI texture lesson too and see if I can can combine them as you suggest.
    Thank you!

  • Really useful, thank you!
    But one thing, when I run it with the First Person Controller, everything stops except the ability to swivel the camera with the mouse or keys. What’s happening there?


    • I don’t really know the reason why it happens but you can easily turn it of by turning off the script components that move your camera, while the game is paused like this (this is just an example, you should modify the code to fit yours):
      or something like that. Hope that helps!

    I’ve been using this code since forever, but if you have objects that are dependent on physics, especially ragdolls,after you resume your game every physics object will go flying around the scene. Is there a workaround for this problem?

  • Thanks for the video, i have a question.. does the timeScale = 0 pauses any animation too?, for instance, here you have the “Jump” key to pause, but if you press .. let say “K” to play some animation, will it be reproduced?..

    Greetings & thanks.

  • I have two questions, one, can you get an object to ignore the time.scale, and what happens if you set it to something like .5?

  • In my game, I have a script to rotate the player to face wherever the mouse is pointing and to shoot whenever you click the left mouse button.

    When I put this pausing script into my game, it paused everything but those two things. How would I go about pausing the rotation of the character and the ability to be able to shoot? (Keep in mind that by shooting, I mean creating a bullet, but it does not go anywhere.)

    I tried making a global variable out of the Boolean, but when I tried referencing in the other scripts that allowed me to do that (so I can make an “if” statement about whether or not it is allowed to shoot), it gave me an error. Is there another way to pause absolutely everything in unity? Or do I have to figure out how to pause that by using global variables and if statements.

    • I think you would want to add the following:

      * Time.fixedDeltaTime

      to any rotation you have.

      so it would be like:

      transform.Rotate(0, 90 * Time.fixedDeltaTime, 0);

      If i understood correctly what he was saying above…

      • or simply add ” if (!paused){ ”
        before any rotations, as long as paused is a global variable

    hey can anyone help me ? i want to make a time counter in my ball game.

    • add Time.deltaTime; to a float value, then check if it has reached a certain number.

      var timer : float;
      timer += Time.deltaTime;
      if(timer > 15){
      //game over!

    Next button takes you to B01, just saying

  • Hi there!
    Great tutorials! Helped me a lot!
    Was it your intention to lead to Challenge 01 (C01) after clicking on “next” on this site (B27)? Because there is module B28 left. When I click on “previous” at the B28 site it redirects me to the correct B27 website.

    Cheers 😉

  • Simply brilliant teaching skills. Thank you so much.

  • Thank you your videos are the most clear concise and you are so calm youve helped me a great deal thank you

  • What if instead of Input.GetKeyUp you want to use OnMouseDown on a particular game object / gui texture?

Leave a Comment