i want to disable a UI button once i already clicked it once, like in hangman game, how is that?
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);
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;
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
}
}
}
very easy
public gameobject yourbutton;
public void DisableButton ()
{
yourbutton.setActive(false);
}
//you have to put this script function in onclick() of the button component