Pause Menu script not working properly
So... I have this problem. It might be even a little funny, but I almost lost all hope for solving it so I'm kindly begging you for help.
This is my pauseScript.cs which is working... in a mysterious way.
First - If the game is on and player will die I'm triggering GameOver function from this script. Game pauses, pausePanel is showing up and I can restart game or go to the main menu. Working like a charm.
The problem is when I want pause game. First of all is remembering my clicks as it happened during the gameplay, so after resuming game my hero will jump like crazy squirrel (which he is not unfortunately cause that way I could have "feature" and not a bug). Second problem is happening after pausing game, resuming and then inevitable GameOver is coming... though it's not :( Player is destroyed, sound is being played, vibration is on... but the camera won't stop, score is still counting and pause menu with gameover text and restart button is not showing up (you need to actually click on it, but even then no restart button is on - only resume one).
Please help me since I'm tweaking this code and it's probably something obvious but it still sneaking out my grasp.
using UnityEngine;
using System.Collections;
public class pauseScript : MonoBehaviour {
public static pauseScript instance;
public bool paused;
public GameObject gameOverText;
public GameObject playButton;
public GameObject replayButton;
[SerializeField]
private GameObject pausePanel;
void Start () {
paused = false;
replayButton.gameObject.SetActive(false);
}
public void Pause(){
paused = !paused;
if(paused){
Time.timeScale = 0;
pausePanel.SetActive(true);
playButton.gameObject.SetActive(true);
replayButton.gameObject.SetActive(false);
gameOverText.gameObject.SetActive(false);
}
}
public void GameOver(){
paused = !paused;
if(paused){
Time.timeScale = 0;
pausePanel.SetActive(true);
playButton.gameObject.SetActive(false);
replayButton.gameObject.SetActive(true);
gameOverText.gameObject.SetActive(true);
}
}
public void ResumeGame(){
paused = paused;
if (paused){
pausePanel.SetActive(false);
Time.timeScale = 1;
}
}
public void GoToMenu(){
Time.timeScale = 1;
pausePanel.SetActive(false);
Application.LoadLevel("02-MainMenu");
}
public void Restart(){
Time.timeScale = 1;
Application.LoadLevel("05-Game");
}
}
Anybody? I must say that after adding, deleting, again adding and again deleting lines in my code - I can't say that I see anything wrong - just simple booleans trying to work perfectly as designed and still... not working.
Answer by GiyomuGames · Nov 10, 2015 at 07:13 AM
Without trying to really understand your logic, I can point out that you put:
public void ResumeGame(){
paused = paused;
I assume you meant paused = !paused;
I would have also thought that it's best to set pause explicitly rather than invert the value, for the sake of safety and clarity.
Ok - that wasn't what I needed but you, kind sir, steered me in the right direction. I managed changed all the paused=paused !paused to just false and true statements and after few tweaks it's working as designed. Thank you for your time and effort!
Now I have to only solve the problem when my inputs (touches) in pause menu are somehow stored and making my player jump after resu$$anonymous$$g game.
Your answer
Follow this Question
Related Questions
Telling the difference between a key press, double press and hold. 0 Answers
OnMouseButtonDown 1 Answer
In-Game Browser, help needed! 1 Answer
Input.GetKeyDown(...) loop glitches 2 Answers
Pause Menu Problems 1 Answer