0
\$\begingroup\$

I am having some issues with the UI and the Input system. What I am trying to do is "simply" to make a menu appear and disappear upon pressing a certain key. To do so, I created an animator attached to the Menu canvas, to which I attached the following script:

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

public class Menu : MonoBehaviour
{
    [SerializeField] Animator anim;

    [Header("Input System")]
    [SerializeField] public GameController controls;
    //[SerializeField] public Vector2 direction;


    private void Awake()
    {
        anim = GetComponent<Animator>();

        controls = new GameController();
    }

    private void OnEnable()
    {
        controls.Enable();

        if (anim.GetBool("isPaused") == false)
        {
            controls.Player.ShowMenu_Start.performed += ctx =>
            {
                anim.SetBool("isPaused", true);
                Debug.Log("Paused");
                //controls.Player.Disable();
                //controls.Menu.Enable();
            };
        }
        
        if (anim.GetBool("isPaused") == true)
        {
            controls.Menu.HideMenu_Start.performed += ctx =>
            {
                anim.SetBool("isPaused", false);
                Debug.Log("Resumed");
                //controls.Menu.Disable();
                //controls.Player.Enable();
            };
        }    
    }
    private void OnDisable()
    {
        controls.Disable();
    }



}

The idea is that when I press the pause key (mapped in the ActionMap of the player i.e. in-game), the menu will be activated, the animation triggered in order to pass to the state transition 'menu activated', the in game ActionMap is disabled and the menu ActionMap enabled.

Vice versa, if the menu is already active, (anim.GetBool("isPaused") == true), I will go the other way around.

So far, the activation of the menu works: I get the "Paused" message in the console, but I can't get to unpause the menu. The bool remains true and additionally upon pressing again, the "Paused" message does not appear.

I tried to comment out the enabling and disabling of the ActionMaps, and this works partially since now the "Paused" message appears every time the key is pressed, but I still cannot set the bool to false. I have also tried to use a single ActionMap but it doesn't work either.

How can I correct this?

EDIT:

Following on the answer by @Kevin, I managed to activate/deactivate the menu. What i intended to do initially, however, was to do so by pressing the same key, like pressing the start button to show up the screen and again, which is why I thought of creating 2 different ActionMaps, so that one would have the action pause and the other the action unpause using the same key. As it is now, the scripts calls the 2 actions at once. I tried again to disable and enable the but without results. I guess that if i disable/enable the gamecontroller, i do it for all of its action maps, is that right? Is there a workaround?

\$\endgroup\$
1
  • \$\begingroup\$ It sounds like you should have one action, "toggle pause", and code like this: if (isPaused) Unpause(); else Pause(); \$\endgroup\$
    – Kevin
    Apr 12 at 18:53

1 Answer 1

1
\$\begingroup\$

Your code is written so that only one event listener will be added. If you're not paused when the menu is enabled, the ShowMenu_Start listener is added. If paused, the HideMenu_Start listener is added when the menu is enabled.

It looks to me like you should just remove the if conditions:

private void OnEnable() {
    controls.Enable();

    controls.Player.ShowMenu_Start.performed += ctx =>
    {
        anim.SetBool("isPaused", true);
        Debug.Log("Paused");
        //controls.Player.Disable();
        //controls.Menu.Enable();
    };
    
    controls.Menu.HideMenu_Start.performed += ctx =>
    {
        anim.SetBool("isPaused", false);
        Debug.Log("Resumed");
        //controls.Menu.Disable();
        //controls.Player.Enable();
    };   
}

Make sure you remove these event listeners in OnDisable().

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the reply! I added another part to the question following you response, but as it stands, you actually answered my initial question, the rest is just a side quest ;) \$\endgroup\$
    – FSic
    Apr 6 at 13:26

You must log in to answer this question.

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