Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why can't I press this button after re-enabling the UI?

Discussion in 'Getting Started' started by shourovkun, Oct 13, 2023.

  1. shourovkun

    shourovkun

    Joined:
    Oct 7, 2023
    Posts:
    5
    I have this UI with buttons that should be clickable whenever the UIDocument is enabled, but for some reason whenever I disable and then re-enable the UIDocument (in the second code block), the buttons are no longer clickable even though the UI shows up. This is the UI code:
    Code (CSharp):
    1. public class UI : MonoBehaviour
    2. {
    3.     public BattleSystem battleSystem;
    4.     void Start(){
    5.         VisualElement root = GetComponent<UIDocument>().rootVisualElement;
    6.        
    7.         Button grenadeButton = root.Q<Button>("Grenade");
    8.         Button pistolButton = root.Q<Button>("Pistol");
    9.  
    10.         grenadeButton.clicked += () => StartCoroutine(battleSystem.useWeapon(Weapon.GRENADE));
    11.         pistolButton.clicked += () => StartCoroutine(battleSystem.useWeapon(Weapon.PISTOL));
    12.     }
    13. }
    And this is the code in the BattleSystem class:
    Code (CSharp):
    1. public IEnumerator useWeapon(Weapon weapon){
    2.         Debug.Log("Button was pressed");
    3.         StopCoroutine(turn); // stops the takeTurn Coroutine
    4.         switch (weapon){
    5.             case Weapon.GRENADE:
    6.                 yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
    7.                 Debug.Log("Powering Grenade");
    8.                 yield return new WaitUntil(() => !Input.GetKey(KeyCode.Space));
    9.                 Debug.Log("Grenade thrown");
    10.                 break;
    11.             case Weapon.PISTOL:
    12.                 yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
    13.                 Debug.Log("Pistol fired");
    14.                 break;
    15.         }
    16.         yield return new WaitUntil(() => Input.GetKeyDown(weaponsKey));
    17.         yield return new WaitUntil(() => !Input.GetKey(weaponsKey));
    18.         StartCoroutine(takeTurn(currentPlayer));
    19.     }
    20. IEnumerator takeTurn(GameObject player){
    21.         currentPlayer = player;
    22.         LoadCameraToPlayer(currentPlayer);
    23.         currentPlayer.GetComponent<PlayerMovement>().enabled = true;
    24.         BattleState currentState = state;
    25.         while (state == currentState){
    26.             yield return new WaitUntil(keyPress);
    27.             switch (pressedKey){
    28.                 case var v when v == passKey:
    29.                     StartCoroutine(TurnEnd()); // changes state to break out of loop
    30.                     break;
    31.                 case var v when v == weaponsKey:
    32.                     yield return new WaitUntil(() => !Input.GetKey(weaponsKey));
    33.                     updateWeaponUIState(!weaponWheel.GetComponent<UIDocument>().enabled); // turn on/off weapon UI upon pressing and releasing the weapons key
    34.                     currentPlayer.GetComponent<PlayerMovement>().enabled = !weaponWheel.GetComponent<UIDocument>().enabled; // player movement
    35.                     break;
    36.             }
    37.         }
    38.     }
    39.     void updateWeaponUIState(bool active){
    40.         weaponWheel.GetComponent<UIDocument>().enabled = active;
    41.         UnityEngine.Cursor.visible = active;
    42.         if (active){
    43.             UnityEngine.Cursor.lockState = CursorLockMode.Confined;
    44.         } else{
    45.             UnityEngine.Cursor.lockState = CursorLockMode.Locked;
    46.         }
    47.         tpc.enabled = !active; //ThirdPersonCamera
    48.         cfl.enabled = !active; //CinemaFreeLook
    49.     }
    The buttons work when I first enable the UIDocument (it starts disabled) but not after disabling and re-enabling it.
    Things I've tried:
    • Removing and adding the listeners back onto the buttons with OnEnable/OnDisable - I don't think you can remove listeners in UIElements so that doesn't work, but I think Start() should be best
    • Disabling the UIDocument GameObject instead of the Component - made no difference
    • Stopping (and not stopping) the takeTurn Coroutine before starting the useWeapon Coroutine. I thought maybe running two related coroutines simultaneously was the problem, it wasn't
    Does anyone have any clue what the issue could be?
     
  2. shourovkun

    shourovkun

    Joined:
    Oct 7, 2023
    Posts:
    5