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?
if (isPaused) Unpause(); else Pause();
\$\endgroup\$