0
\$\begingroup\$

I am having trouble understanding how to trigger a UI button with a specific joystick key only.

Currently i have a pause menu in which i have several buttons. By pressing start i set the game time scale to 0 and activate the menu using this:

void Update()
    {
        if (Input.GetButtonDown("Submit"))
        {
            ResumeStartButton();
        }
    }

public void ResumeStartButton()
    {
        isPaused = !isPaused;
        if (isPaused)
        {
            pausePanel.SetActive(true);
            Time.timeScale = 0f;
            usingPausePanel = true;
            player.SetActive(false);
        }
        else
        {
            pausePanel.SetActive(false);
            Debug.Log("Resume game with start button");
            Time.timeScale = 1f;
            usingPausePanel = false;
            player.SetActive(true);
        }

    }

Once the menu is active, i can navigate through the buttons with the joystick, however the buttons respond only to the "Submit" button i defined earlier.

I tried using this function:

public void Resume()
    {
        if (Input.GetButtonDown("MenuSelect_A")) 
        { 
                isPaused = !isPaused;
            if (isPaused)
            {
                pausePanel.SetActive(true);
                Time.timeScale = 0f;
                usingPausePanel = true;
                player.SetActive(false);
            }
            else
            {
                pausePanel.SetActive(false);
                Debug.Log("Resume game");
                Time.timeScale = 1f;
                usingPausePanel = false;
                player.SetActive(true);
            }
        }

    }

And call it using an OnClick event on the editor, however it does not work. I have also tried to use onClick on the script directly by creating resumeButton.onClick.AddListener(Test) and by using AddListener:

void Start()
    {
        isPaused = false;
        pausePanel.SetActive(false);
        inventoryPanel.SetActive(false);
        usingPausePanel = false;


        resumeButton.onClick.AddListener(Test);


    }

public void Test() 
    {
        if (Input.GetButtonDown("MenuSelect_A"))
        {
            Debug.Log("Do something you donkey!");
        }
        else
        {
            Debug.Log("Wrong button");
        }
    }

However this does not work either: While the menu is active none of the messages appear and when i exit the menu using the start button only the second sentence, "Wrong button" appear.

Can somebody tell me what am i doing wrong? I think that there should be an easy way to solve this with OnClick events but i don't understand what precisely.

For information, I am using the old input manager. I was considering switching to the new system since my project is in still in its beginning.

Any tip will be appreciated!

\$\endgroup\$
4
  • \$\begingroup\$ So what is calling your Resume()? Are you calling it from Update()? If nothing is ever calling Resume(), how can the check of the buttons inside be called? \$\endgroup\$
    – Zibelas
    Jun 19, 2021 at 11:37
  • \$\begingroup\$ Right now i replaced Resume() with Test() just to see if at least i was able to make it work, but neither Resume() nor Test() did. On top of that, at first i tried to put the line resumeButton.onClick.AddListener(Test); in Update() but it kept being called, that is why i put it in Start(). \$\endgroup\$
    – FSic
    Jun 19, 2021 at 12:24
  • 1
    \$\begingroup\$ It isn't clear but I assume you are using the normal Input Manager and not the new Input system. Do you have a MenuSelect_A assigned in the Input Manager and does it correspond to the button you push on the controller? Or is MenuSelect_A the name of the UI element you expect to be clicked? \$\endgroup\$
    – James West
    Jun 24, 2021 at 19:07
  • \$\begingroup\$ Exactly, I am using the old Input manager. MenuSelect_A is the key I want to use and I assigned it in the Input Manager. Do you think that switching to the new Input system would help with this issue? \$\endgroup\$
    – FSic
    Jun 25, 2021 at 12:06

1 Answer 1

0
\$\begingroup\$

In the end I solved the problem by switching to the new Input System:

  • I created a dedicated action map for the UI menus (the 'pauseMap') and one for the player control 'playerMap',
  • Whenever I press the start button from the 'playerMap', it activates the menu and now the gamepad buttons are used to move through the menu and press buttons (then each button do its job with 'OnButtonClick' events),
  • When I press the start button from the 'pauseMap' it disables the menu and switches back to 'playerMap'.
\$\endgroup\$

You must log in to answer this question.

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