- Home /
Pausing the game is not working
I know this question has been asked before many a time, but I'm creating the simplest of scripts. So far it's the only script because I started a new project. The script allows you to press a key to open the inventory, which works easily enough, because it's simple. What's not simple is that I need the game to pause when this happens. To test whether or not the game's paused I created a cube and attached the simplest of rotation scripts to it, the idea being if the game pauses it should stop spinning, right? Well it doesn't. This is getting on my nerves now quite frankly. I'm using the following code in the Update function to pause and unpause: (C#)
if(inventoryOpen == true)
Time.timeScale = 0;
if(inventoryOpen == false)
Time.timeScale = 1;
Could anyone please tell me why this isn't working? Many thanks.
a) script not enabled? b) not assigned on a gameobject c) where are you calling this from? and does that work?
Have you tried inserting debug statements to make sure the code is actually being called? (i.e Debug.Log("Inventory open, so we should be paused");
Answer by Nimred · Jan 06, 2015 at 04:19 PM
From the scripting API: "When timeScale is set to zero the game is basically paused if all your functions are frame rate independent." (...) "FixedUpdate functions will not be called when timeScale is set to zero."
When you set timeScale to 0, FixedUpdate functions will not be called but Update functions will be.. The idea is that your Update functions are supposed to be frame-rate independent, which means every movement like your cube rotation should make use of Time.deltaTime to calculate how much it should move. Am I correct in assuming your cube rotation is rotating without using a deltaTime? If so, it would not be paused.
Answer by Kamuiyshirou · Jan 06, 2015 at 03:19 PM
Use textures to pause and continue the game:
using UnityEngine;
using System.Collections;
public class PausarGame : MonoBehaviour {
public GUITexture pauseTextura;
public GUITexture continueTextura;
void Start () {
continueTextura.enabled = false;
pauseTextura.enabled = true;
}
void Update() {
if (pauseTextura.HitTest (Input.mousePosition)) {
Time.timeScale = 0;
continueTextura.enabled = true;
}
if (continueTextura.HitTest (Input.mousePosition)) {
Time.timeScale = 1;
continueTextura.enabled = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613175700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Windows store app] How to disable pause game when lost focus? 0 Answers
resuming Time.timeScale Problems 2 Answers
how to stop player movement and look when paused 3 Answers
Resuming Application on Android 2 Answers
Saving jump power/Pause controller 1 Answer