- Home /
Pause Menu Buttons Not Working on First Attempt
Hi guys,
I need help with my Pause Menu I created. I made a script where if you hit the Escape button, the pause menu will appear and they'll be two buttons; home and resume. My mouse cursor hovers over the buttons and the two buttons should be highlighted and work, however it doesn't work properly.
What I mean by that is, the buttons do not become highlighted nor do they work. If I try to click on the buttons, my mouse will disappear and I can't do anything. I have to hit the Escape key to exit out of the Pause Menu and back into my scene. Then if I hit the Escape key again the Pause Menu will appear and now if I hover my mouse over the buttons they become highlighted and if I can click on any of them they start working. How do I fix this issue?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour {
public Transform PauseCanvas;
public Transform Player;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
Resume();
}
}
public void Resume()
{
if (PauseCanvas.gameObject.activeInHierarchy == false)
{
PauseCanvas.gameObject.SetActive(true);
Time.timeScale = 0;
Player.GetComponent<FirstPersonController>().enabled = false;
}
else
{
PauseCanvas.gameObject.SetActive(false);
Time.timeScale = 1;
Player.GetComponent<FirstPersonController>().enabled = true;
}
}
public void Home()
{
SceneManager.LoadSceneAsync("Home");
}
}
Answer by Vega4Life · Dec 10, 2018 at 03:37 AM
Try to force it on...
Cursor.visible = true;
Just put this underneath where you are turning the canvas on. If you still don't see it, then somehow your canvas my be having some z-fighting with the cursor.
Hi @Vega4Life
I updated my question, because I wasn't clear. I see my mouse cursor and I can hover over my buttons. However, the buttons do not work. They do not become highlight nor do they work. If I try to click on the buttons, my mouse will disappear and I can't do anything. I have to hit the Escape key to exit out of the Pause $$anonymous$$enu and back into my scene. Then if I hit the Escape key again the Pause $$anonymous$$enu will appear and now if I hover my mouse over the buttons they become highlighted and if I can click on any of them they start working. How do I fix this issue?
Ohk, What you can do is when you are in unity, play the game and select the pause menu game object while in play mode, check if every thing is enable by defalut.
You can also enable everything in a function and whenever you pause the game call the enabling function.
For Example: In
public void Resume()
{
if (PauseCanvas.gameObject.activeInHierarchy == false)
{
EnableAllComponents();
Time.timeScale = 0;
Player.GetComponent<FirstPersonController>().enabled = false;
}
}
void EnableAllComponents()
{
button1Object.SetActive(true);
button2Object.SetActive(true);
//.. And all the other pause menu components.
PauseCanvas.gameObject.SetActive(true);
}
Hi @darkStar27
Question 1 In regards to using LoadSceneAsync, I believe I read somewhere that LoadSceneAsync would be helpful for loading scenes that have a lot of content in them. However, all my scenes I've created in Unity have never cause me any problems, or slowed down my computer. Do you suggest I change: Scene$$anonymous$$anager.LoadSceneAsync("Home");
to something else?
Question 2: Yes, this is a FPS game. I'm using the standard Unity's FPS Controller.
Okay, then that's understandable. I won't worry about that then.
Answer by darkStar27 · Dec 10, 2018 at 04:09 AM
First of all tell me why are you using LoadSceneAsync instead of LoadScene or SetActiveScene.
Possible Reason
Coming to the problem at hand, if your game is a First Person Shooter, then when press Esc for the first time it is to show the mouse cursor (which is common with almost every FPS asset). So, you need to press Esc twice in order for it to work.
You can dig in the script files of you imported asset to set some other key to show mouse, or maybe add the pause menu code there to work at the same time.
.
Possible Solution
The easiest way would be to create a onscreen dedicated button for menu, or use buttons like Tab or Tilde as they aren't used in anything.
Another Reason
If its not a FPS game, try to map a different button instead of Esc, and check if the problem still occurs.
Your answer
Follow this Question
Related Questions
Pause Menu open/close on key 1 Answer
iTween issue creating a gui. menu box 1 Answer
Button highlighted a pushable without touching it 0 Answers
Tutorials for 2D game GUI 2 Answers
Pause menu not working 1 Answer