- Home /
Time.timeScale doesn't stop void update() from beeing run
Hello,
I'm working on an mobile Android game. It's somewhat similar in mechanics to flappy bird in that each attempt is not going to last very long so I want as little as possible between the player and the next attempt. I just basically want to present the score and a text saying something along the lines of "Press here to try again", and then a new try is initiated.
The way I've tried to solve this is by making the whole screen a GUI.Button and the game being paused until it is pressed with Time.timeScale = 0. It works fine although the update function of one of my classes is still being run, I dont understand why and it also messes up the next attempt.
First I have an "event manager" that other classes register certain functions with so that I only need to call the trigger function in the event manager, which then calls all the functions registered with it.
public static class GameEventManager{
public delegate void GameEvent();
public static event GameEvent GameStart, GameOver, GamePaused, GameUnpaused, GameQuit;
public static void TriggerGameStart(){
if(GameStart != null){
GameStart();
}
}
public static void TriggerGameOver(){
if(GameOver != null){
GameOver();
}
}
}
I then have a GUI manager class that displays text. This is also the class that presents the button inbetween attempts. It looks like this:
using UnityEngine;
public class GUIMan : MonoBehaviour {
private static GUIMan instance;
public GUIText scoreText, gameOverScoreText;
private bool paused;
private int score;
// Use this for initialization
void Start () {
paused = true;
instance = this;
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
gameOverScoreText.enabled = true;
scoreText.enabled = true;
Time.timeScale = 0;
PrintGameOverText();
}
// Update is called once per frame
void Update () {
}
void GameStart(){
paused = false;
Time.timeScale = 1;
gameOverScoreText.enabled = false;
scoreText.enabled = true;
}
void GameOver(){
paused = true;
Time.timeScale = 0;
PrintGameOverText();
}
void PrintGameOverText(){
instance.gameOverScoreText.enabled = true;
instance.gameOverScoreText.text = "Your score was: " + score.ToString();
//instance.scoreText.enabled = false;
}
public static void SetScoreText(int sco){
instance.score = sco;
instance.scoreText.text = "Score: " + sco.ToString();
}
void OnGUI(){
if(paused){
GUI.backgroundColor = Color.clear;
if(GUI.Button (new Rect(0, 0, Screen.width, Screen.height), "")){
GameEventManager.TriggerGameStart();
}
}
}
}
The game is controlled via touch of course. I have like an analog stock that follows the thumb when pressed on the screen. The drawing of the analog stick is done in the OnGUI function of the controller class and the calculations of it's position is done in the update function of the same class. The problem is that even tough the game is paused via Time.timeScale = 0, the update and OnGUI functions of the controller classes are still being run. When the score is shown, all of my rigidbody objects stop, but i can still touch the screen and my analog stick follows my thumb.
The weird thing is, I use the same method when the game first starts, all objects are frozen and some welcome text is presented and here the analog stick is also frozen until I press the GUI.Button by touching the screen. The problem arises every time after that. However with some more testing I've found out that the problem is there the first tima as well, only it's not visible.
Does anybody know what I could be missing? I would be grateful for any help!
Thank you
Answer by fafase · Mar 01, 2014 at 12:16 PM
Update and OnGUI are independent of the time scale, they run via the OS. Only your FixedUpdate is stopped and all physical operations.
You can easily fix that with:
void Update(){
if(Time.timeScale == 0)return;
}
Or you can come up with a proper game manager:
public enum State{
Pause, Running
}
public class GameManager():MonoBehaviour{
private State state;
public void SetState(State state){this.state = state;}7
public State GetState(){return state;}
}
and then you add the component and access the value of the state.
I feel like such an idiot though since the solution was really simple... I will look into your game manager idea but at the moment the first tip will do!
Thank you!
Point of the game manager you can have different states without altering the timescale. For instance, you want to finish the game but play an anim, putting timescale to 0 would stop the anim, while a simple gameOver state would do. Also, using flags you can cumulate states, like gameOver and won or game over and loss.
Your answer
Follow this Question
Related Questions
how do I make it so that my game only starts after the player presses Jump? 0 Answers
How to pause and unpause my game - Input.GetAxis doesn't work when Time.timeScale is 0 3 Answers
solution to detect multi- finger touch position 2 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Level completed but still playable (timescale problem) 0 Answers