disable UI button
i want to disable a UI button once i already clicked it once, like in hangman game, how is that?
Answer by Dustin_00 · Jun 10, 2020 at 02:04 AM
This leaves the button on the screen with the disabled color and the user can't click it:
yourButton.interactable = false;
This leaves the button on the screen, the user can't click it, but does NOT use the disabled color:
yourButton.enabled = false;
This removes the button from the UI entirely:
yourButton.gameObject.SetActive(false);
Answer by Bladynator · Aug 06, 2016 at 08:47 AM
You might just want to make it non-interactable (then the button stays, but turn grey by default). You do this from your script by putting this in the script of the object where the button is located:
GetComponent<Button>().interactable = false;
Don't forget to add using UnityEngine.UI; at the beginning
Answer by mafanbong8819 · Feb 09, 2017 at 07:20 PM
try this script. is working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class disable : MonoBehaviour {
//GUI btn = (GameObject)Instantiate(Resources.Load("MyButton"));
//GameObject btn = GameObject.CreatePrimitive(PrimitiveType.Plane);
//public Object btn = Object.Instantiate(Resources.Load("MyButton"));
//public Color newColor;
//public Button mybutton;
//public Color newColor;
public Sprite blockA;
public Sprite blockA_disable;
public bool condition;
public Button mybutton;
public int counter;
void start()
{
mybutton = GetComponent<Button> ();
}
public void changebutton()
{
counter++;
if (counter % 2 == 0) {
mybutton.image.overrideSprite = blockA;
//condition for working button.
} else {
mybutton.image.overrideSprite = blockA_disable;
//condition for disable button
}
}
}
Answer by R4y-GM · Nov 30, 2018 at 06:24 PM
very easy
public gameobject yourbutton;
public void DisableButton ()
{
yourbutton.setActive(false);
}
//you have to put this script function in onclick() of the button component
Except that DEACTIVATES the button, not DISABLES it
Your answer
Follow this Question
Related Questions
Button Flickering On and Off When Pressed 2 Answers
button pressing the button next to it?! 2 Answers
Android UI push Multiple buttons Help 0 Answers
Possible to long/short press a UI button - Phone device 0 Answers