- Home /
How to toggle between paused mode and running mode?
Hello guys, I'm trying to code a script to toggle between the game while paused and the game while running by pressing the P key. I already coded a function to do that, but the game is never paused, keeps running like if nothing happened.
bool trocar = false;
// Update is called once per frame
void Update () {
Trocar();
}
void Trocar () {
if (Input.GetKeyDown(KeyCode.P)) {
if(trocar == false) {
Time.timeScale = 0;
Time.fixedDeltaTime = 0.02f * Time.deltaTime;
trocar = true;
}
if(trocar == true) {
Time.timeScale = 1;
Time.fixedDeltaTime = 0.02f * Time.deltaTime;
trocar = false;
}
}
}
In this function, when I press the P key, it tests if the variable trocar is true or false. If it's false, then it pauses the game, if it's true, it puts the game running again. The problem is that if(trocar == true) always run after if(trocar == false), never pausing the game. What changes I need to do to fix this?
Answer by Khada · Mar 10, 2013 at 02:42 PM
Replace:
if(trocar == true)
With:
else
EDIT:
Actually you can shorten it even more by doing it like:
if(!trocar)
{
//...
}
else
{
//...
}
Your answer
Follow this Question
Related Questions
Audio Toggle Background Music (With Trigger ) 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Pause Menu Problems 2 Answers
Pause menu scripting help? 1 Answer