0
\$\begingroup\$

I have a small issue with my UI. Whenever I click a button on my pause menu ui nothing happens even when I hover over the buttons. However, in my main menu scene, the UI works without any problems. Comparing the code side by side they are pretty much identical. I am also using the UI toolkit for my UI instead of the older UI system.

public class PauseMenuUIDocumentManager : MonoBehaviour
{
   private static VisualElement pauseMenuContainer;
   private Button mainMenuButton;
   private Button settingsButton;
   private Button quitToDesktopButton;

   public static VisualElement PauseMenuContainer
   {
      get => pauseMenuContainer;
      set => pauseMenuContainer = value;
   }

   private void Start()
   {
      var root = GetComponent<UIDocument>().rootVisualElement;

      pauseMenuContainer = root.Q<VisualElement>("Background");

      mainMenuButton = root.Q<Button>("MainMenuButton");
      settingsButton = root.Q<Button>("SettingsButton");
      quitToDesktopButton = root.Q<Button>("QuitToDesktop");
      
      mainMenuButton.clicked += MainMenuButtonOnclicked;
      settingsButton.clicked += SettingsButtonOnclicked;
      quitToDesktopButton.clicked += QuitToDesktopButtonOnclicked;

      
   }

   private void QuitToDesktopButtonOnclicked()
   {
      Application.Quit();
   }

   private void MainMenuButtonOnclicked()
   {
      Time.timeScale = 1f;
      GameManager.LoadScene("MainMenu");
   }

   private void SettingsButtonOnclicked()
   {
      throw new NotImplementedException("This feature will be implemented later down the line");
   }
}

Here is my game manager script that actually shows the pause menu:

if (Input.GetKeyDown(KeyCode.Escape) && gameIsPaused == false && gameCanBeUnPaused == false)
      {
         PlayerMovement.MovementEnabled = false;
         gameIsPaused = true;
         Time.timeScale = 0f;
         StartCoroutine(CheckIfGameCanBeUnPaused());
         print("The game is paused");
         OpenPauseMenu();
      }
private void OpenPauseMenu()
   {
      PauseMenuUIDocumentManager.PauseMenuContainer.style.display = DisplayStyle.Flex;
   }
\$\endgroup\$
3
  • \$\begingroup\$ Why are you using a coroutine here? 'StartCoroutine(CheckIfGameCanBeUnPaused())' \$\endgroup\$
    – Zibelas
    Dec 26, 2023 at 21:49
  • \$\begingroup\$ I'm using a routine because I could not find another way to use the escape key to open and close the pause menu. Looking at it now there's a for sure better way to do this. \$\endgroup\$
    – Miracle
    Dec 27, 2023 at 1:07
  • \$\begingroup\$ I can also say the use of the coroutine is not the issue \$\endgroup\$
    – Miracle
    Dec 27, 2023 at 1:15

1 Answer 1

0
\$\begingroup\$

Well, I found the problem. The problem was that you CANNOT have two UI documents active in the same scene that overlap, and if you do you have to disable the other one before you can interact with the other UI elements.

\$\endgroup\$

You must log in to answer this question.

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