how can I disable the button and then re-enable it?
Use the property interactable.
button.interactable = false;
button.interactable = true;
You did not specify what language you where using. That may be why you have problems getting other answers to work.
Here is an example for C# (file name is .cs)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class disablebutton : MonoBehaviour {
public Transform button; //Drag a button transform on to here, and see how to toggle it when Space is pressed
void Update () {
if (Input.GetKeyUp(KeyCode.Space) )
{
//If space is pressed, and the button is currently interactable, make it so its not interactable
if (button.GetComponent<Button>().IsInteractable() == true)
{
button.GetComponent<Button>().interactable = false;
}
else //Else make it interactable
{
button.GetComponent<Button>().interactable = true;
}
}
}
}
And same script in UnityScript (filename .js)
import UnityEngine.UI;
var button : Transform; //Drag a button transform on to here, and see how to toggle it when Space is pressed
function Update () {
if (Input.GetKeyUp(KeyCode.Space) )
{
//If space is pressed, and the button is currently interactable, make it so its not interactable
if (button.GetComponent(Button).IsInteractable() == true)
{
button.GetComponent(Button).interactable = false;
} else {
button.GetComponent(Button).interactable = true;
}
}
}
I had to add some parts that are spread out among the answers and comments to get the whole answer as this was also a question that I needed answered. First at the top of the script you need to put:
using UnityEngine.UI;
The next part is a bit trickier but the way that I used was that I have the script in question actually on my button so in the function that clicking the button activates I added this line of code.
this.GetComponent(LessThan)Button(GreaterThan)().interactable = false;
In the function which should then return usability to the button I added the mirror line of code.
this.GetComponent(LessThan)Button(GreaterThan)().interactable = true;
I hope this helps anyone that is looking for help with this issue going forward. I had to add the (LessThan)Button(GreaterThan) portion b/c the symbols are used by the website to perform actions.
You need to call ComponentInChildren like this.
putNextButton.GetComponentInChildren().interactable = false;
Works like a charm for me. Switch to true; when you want it to be enabled again.
Don’t forget to include, using UnityEngine.UI; up top!
Cheers!
Is there a basic way to make a button invisible after a event in the game engine? I was hoping the new UI might address this question, thanks
I would like to add to the topic as there seems to be some confusion (a gotcha that got me)…
There are Two ways to deactivate a button with the new UI that I found are helpful.
One is to completely eliminate the button OBJECT from the game (ie: you don’t even see it). In order to do this you can do the following:
Using UnityEngine.UI;
...
...
public GameObject buttonIWantToHide; //make sure to drag the UI button from your hierarchy into the script
void Start() {
buttonIWantToHide.SetActive(false); //makes the button invisible
}
void Update() {
if (...end of level, HP = 0, etc...) {
buttonIWantToHide.SetActive(true);}
}
Now if you want the button itself to be SELECTABLE ie: can they interact with it, you can add the following:
void Update() {
if (...end of level, HP = 0, etc...) {
buttonIWantToHide.SetActive(true);
}
if (button is now visible but I don't want player to select it yet) {
buttonIWantToHide.GetComponent<Button>().interactable = false; // this will show the button but greyed out if you leave it at the default settings in your Button UI
}
}
The purpose of interactable can be for example if the player did not earn enough points, he/she can’t post their score to the leaderboards, or they can’t go to the next level. You want to show them at they completed the level, but not the requirement to get the button interactable.
Hope this helps, that’s the logic I use when configuring buttons
I cannot get the grayed out image using interactable=false. What am I doing wrong? I even tried adding enabled = false and still don’t get the grayed out image (I am doing sprite swap for these). It doesn’t let me interact with the button, but I really want a different color there.
qBtn.interactable = false;
qBtn.enabled = false;
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // required when using UI elements in scripts
public class Example : MonoBehaviour {
public Button startButton;
public bool playersReady;
void Update ()
{
// checks if the players are ready and if the start button is useable
if (playersReady == true && startButton.interactable == false)
{
//allows the start button to be used
startButton.interactable = true;
}
}
}
- Make sure your have using UnityEngine.UI;
- Then set your variable public Button startButton;
- Finally call the action startButton.interactable = true;
TIP* NOT button.isInteractble
For GUI Elements created through a Script the following works for me.
GUI.enabled=m_UserStatus.allReady();
if (GUI.Button (new Rect (lx + 0.8f * lw, ly + lh - 64, 0.1f * lw, 32), "Start")) {
start ();
}
GUI.enabled=true; // to enable it for the next elements afterwards
I used something like this, and it works
PointsText.canvasRenderer.gameObject.SetActive(false);
Notice that ‘PointsText’ is instance of UI’s Text class.
I know the question is old, but there is a better way to use the interactable property without declaring another gameobject or using Finds:
myBtn.GetComponent<Button>().interactable = false;
After allot of tries I found the solution:
GameObject myButton;
myButton = GameObject.Find ("Button");
myButton.GetComponent().interactable = false;
myButton.GetComponent().interactable = true;