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");
        }
    }

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.


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.