- Home /
Question by
zymagoras · Feb 26, 2018 at 08:26 PM ·
timescalepause gameresume
Pause/unpause with input.getkeydown issue
Hi all, could anyone explain why this code works with "else if" but doesn't work if it is only "if". Thanks.
void Start () {
restart.onClick.AddListener(restart_game);
exit.onClick.AddListener(exit_game);
resume.onClick.AddListener(resume_game);
mainScreen.onClick.AddListener(main_screen);
isGameOver = FindObjectOfType<collision_detect>().is_game_over;
game_paused = false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Escape))
{
if(game_paused == true)
{
restart_button.SetActive(false);
exit_button.SetActive(false);
gameOver.SetActive(false);
resume_button.SetActive(false);
game_paused = false;
Time.timeScale = 1;
}
else if(game_paused == false)
{
gameOverText.text = "game paused";
restart_button.SetActive(true);
exit_button.SetActive(true);
gameOver.SetActive(true);
resume_button.SetActive(true);
game_paused = true;
Time.timeScale = 0;
}
}
Comment
Best Answer
Answer by TreyH · Feb 26, 2018 at 09:08 PM
Are you asking why it doesn't work properly if that else if
block was removed? Well, you wouldn't have anything to change your game_paused
from false
to true
, as it could only go from true
to false
.
If you're wanting to have a single block, you could do:
if(Input.GetKeyDown(KeyCode.Escape))
{
game_paused = !game_paused;
restart_button.SetActive(game_paused);
exit_button.SetActive(game_paused);
gameOver.SetActive(game_paused);
resume_button.SetActive(game_paused);
Time.timeScale = (game_paused) ? 0 : 1;
}
Answer by zymagoras · Feb 27, 2018 at 08:14 PM
Thanks man, it took me few minutes to digest what you said and now when i look at my code i feel retarded :D
Your answer
Follow this Question
Related Questions
Time.timeScale doesnt work with GUI? 0 Answers
Making a pause menu resume 2 Answers
Android back button / Pause and exit game 0 Answers