0
\$\begingroup\$

Hi I'm working on a game for a school project but I'm still pretty new at game dev, I decided to add a pause menu to my game but for some reason, it only works in one of the scenes in unity, and I can't tell why it won't work on any of the other ones

Here is a video showing the problem: https://youtu.be/28fD9UEtfpg

and here is my script just in case

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;

public GameObject PauseMenuUI;


// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        if (GameIsPaused)
        {
            Resume();
        }
        else
        {
            Pause();
        }
    }
}

public void Resume()
{
    PauseMenuUI.SetActive(false);
    Time.timeScale = 1f;
    GameIsPaused = false;
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

void Pause()
{
    PauseMenuUI.SetActive(true);
    Time.timeScale = 0f;
    GameIsPaused = true;
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
}
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Some of the endless possibilities:

  • You accidentally deleted the GameObject with the Event System component (as noted by DMGregory, this is not visible in the scene hierarchy in the second half of the video)
  • An invisible UI element (such as an image with alpha set to 0) is covering the pause menu and blocking raycasts
  • You have a canvas group in the pause menu hierarchy somewhere and accidentally unchecked "interactable" or "blocks raycasts"
  • The canvas is missing a Graphic Raycaster
  • "Raycast target" is unchecked on the button UI components in the Inspector
  • You're getting some error that prevents the UI from working. Check the Console for errors (the Console is not visible in the video).

It looks like your code is working, since the cursor gets unlocked when the pause menu appears.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Check the video - they're missing an EventSystem in the second scene. 😉 \$\endgroup\$
    – DMGregory
    Apr 9, 2021 at 23:06
  • \$\begingroup\$ @DMGregory I thought I did check that, but I must have accidentally looked at the end of the first scene. To be fair, the EventSystem could nested in something else, but it's more likely they accidentally deleted it. \$\endgroup\$
    – Kevin
    Apr 9, 2021 at 23:13
  • \$\begingroup\$ Yeah, it was the event system I might have accidentally deleted it at one point, thank you very much! \$\endgroup\$ Apr 9, 2021 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .