Help with Pause Menu
I'm trying to get my pause menu to open but it won't.
I followed this tutorial and it seems to work for him but not for me. https://www.youtube.com/watch?v=xIevsYimJYc https://www.youtube.com/watch?v=TatAnGj1RMg
I can't get the script to appear in the functions when try to assign them in a canvas' buttons either. I don't know if that would be why or not.
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
public GameObject PauseUI;
private bool paused = false;
void Start()
{
PauseUI.SetActive (false);
}
void Update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if(paused)
{
PauseUI.SetActive(true);
Time.timeScale = 0;
}
if(!paused)
{
PauseUI.SetActive(false);
Time.timeScale = 1;
}
}
public void Resume()
{
paused = false;
}
public void MainMenu()
{
Application.LoadLevel(1);
}
public void Quit()
{
Application.Quit();
}
}
Answer by Dibbie · Nov 19, 2015 at 09:24 AM
Try if(paused == false) instead of if(!paused), I ran into this problem with my pause screen too, I have ZERO idea why it matters to Unity, but it seemd to somehow register paused == false verse paused not equal to itself... Again, no idea why.
Still nothing.
I tried updating the code like this by messing with the paused = !paused to what you said and adding in another if(Input.GetButtonDown("escape")) but changed the Button to $$anonymous$$ey. Still nothing.
It's still not appearing in the canvas/buttons functions.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Pause$$anonymous$$enu : $$anonymous$$onoBehaviour {
public GameObject PauseUI;
private bool paused = false;
void Start()
{
PauseUI.SetActive (false);
}
void Update()
{
if (Input.Get$$anonymous$$eyDown ("escape")) {
paused = true;
}
if (paused) {
PauseUI.SetActive (true);
Time.timeScale = 0;
}
if (paused == false) {
if (Input.Get$$anonymous$$eyDown ("escape")) {
PauseUI.SetActive (false);
Time.timeScale = 1;
}
}
}
public void Resume()
{
paused = false;
}
public void $$anonymous$$ain$$anonymous$$enu()
{
Application.LoadLevel(1);
}
public void Quit()
{
Application.Quit();
}
}
Hmm, try checking the Layer as well, and make sure your Pause UI is a higher number then your others, so it shows up first.
Also, for "escape", try changing the escape key lines to a keycode:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
Thats weird, I just ran your code in a new project, made a very basic Canvas with a green Panel, nice and simple
Then put the entire Canvas as the "game object"/"pause UI".
Pressing Escape, pops the menu up, and because you have "pause = true" on escape, this Canvas stays open, but your code does work fine... I also created a second Canvas with the Sort Order (not Layer, my bad), of 5 for example (higher then the "UI Canvas"), and the UI one to 0. The UI one never shows up, but the code still works, now flipping this, the UI being 5, and the other second UI I made as 0, the first one (the menu) actually appears above it. Whats happening for you trying this?
Okay. I got it to pop up. It stays up on button press.
The solution was that I had the script on the $$anonymous$$ain Canvas/PauseUI while the buttons On Click() GameObject set to the $$anonymous$$ain Camera.
Upon setting the script to the Camera, it pops up on pressing escape.
Now, any idea how I would get it close? Would I need to go back to !pause?
Also, thank you very much for your assistance.
Edit: On further inspection, the resume button doesn't work. I'm not sure about the quit button because I've only built it into the Unity Engine. It should work fine, though.
On a side note, the $$anonymous$$ain $$anonymous$$enu button works but when I click play after going back to the menu (through clicking in the pause menu), it loads the first level and the music but neither the characters nor the enemies move.
You COULD use pause = !pause again every time they press the key, you could also do a logical switch if you want, like, when they press escape:
if(time.timeScale == 0){
//we know the games paused, so unpause it
pause = false;
}
else{
//We know the game has to be unpaused, so pause it
pause = true;
}
If your button is set up correctly, the OnClick() event, is referencing the object that has the script on it, then there should be no problems, you can test it with a print("exiting game...");
line or something alike, so you can check your console when you press it, just to make sure it IS actually running, cause then you know it would actually exit later on in the build.
Same thing for resume, make sure its on the button, for resume, you want to do more then just set pause to false, you also want to reset the timeScale, OR take the code of the key press outside your false pause check in your Update, because what your update will do is, if paused is false, which lets assume it is... ONLY WHEN THEY PRESS Escape during that time pause is still false, then resume the game... But if paused is true, your other if statement will run right after... So it will remain paused again.
And the movement thing could be due to timeScale, try just resetting it before loading the level again. time.timeScale = 1;