Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to disable a button using the new input system?

Discussion in 'Input System' started by LeoFeitosa, Aug 13, 2020.

  1. LeoFeitosa

    LeoFeitosa

    Joined:
    Jul 5, 2017
    Posts:
    32
    Hello everyone.

    I need to disable a specific button, at a certain moment I want to prevent the player from having any action when pressing a certain button, but I'm not sure how to do this, could you help me?
     
  2. lowwailam

    lowwailam

    Joined:
    Oct 15, 2019
    Posts:
    1
    Copied from Unity documentation. There already is an existing feature by Unity to let you change whether a button in interactable.

    https://docs.unity3d.com/540/Documentation/ScriptReference/UI.Selectable-interactable.html

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI; // required when using UI elements in scripts
    4.  
    5. public class Example : MonoBehaviour {
    6.  
    7.     public Button startButton;
    8.     public bool playersReady;
    9.  
    10.  
    11.     void Update ()
    12.     {
    13.         // checks if the players are ready and if the start button is useable
    14.         if (playersReady == true && startButton.interactable == false)
    15.         {
    16.             //allows the start button to be used
    17.             startButton.interactable = true;
    18.         }
    19.     }
    20. }
     
  3. LeoFeitosa

    LeoFeitosa

    Joined:
    Jul 5, 2017
    Posts:
    32
    I was referring to the joystick button