- Home /
NewUI 4.6 how to change button state to disable?
how can I disable the button and then re-enable it?
When I change the status of the button to-from interactable in code, the color of the button in the game does not actually change until I try to press it or another of the buttons in my canvas. Is there some sort of method to call a forced update on the colorblock lookup when it should be changing?
Answer by Kaz_Yamof · Sep 05, 2014 at 11:29 AM
Use the property interactable. button.interactable = false; button.interactable = true;
This doesn't seem to show up on the $$anonymous$$onoDevelop auto complete options, which makes me think that I've got something set up incorrectly.
I have done the following to try to access a button's intractability where my hierarchy is...
Canvas > Panel > Button.
I have tried this sort of code...
GameObject myButton;
myButton = GameObject.Find ("Button");
myButton.button.interactiable = false;
Are UI objects different from other GameObjects? I tried using something like this too...
myButtonScript = myButton.GetComponent<Button>();
myButtonScript.interactable = false;
... but the Button (Script) is eluding me. It's not a known type for GetComponent(); How do I grab a hold of it?
Answer by Linus · Nov 13, 2014 at 11:48 AM
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;
}
}
}
Answer by judy3turn · Apr 10, 2015 at 01:56 AM
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;
Weird. Try just put a new fresh button on scene and change Interactable by inspector to see if the gray will appear. Here is ok.
Is your qBtn setup as a GameObject or a Button?
At the top of your script, do you see:
public GameObject qBtn;
OR
public Button qBtn;
If you have a gameobject, you have to get it's component Button by doing:
qBtn.GetComponent<Button>().interactable = false;
Button transition should be Color Tint. If it is set to Sprite Swap and there is no disabled sprite then you can not see any changes.
Answer by Mosy · Oct 10, 2014 at 01:31 AM
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.
Why would you vote this down, this is the correct way to do it assu$$anonymous$$g that the script is on the button. Voting down should only occur if the text in the comment would not work. Not because you like someone else's answer better or because you already have an answer on it and don't want people seeing other ways of doing it.
Answer by lioralterman · Sep 22, 2014 at 05:03 PM
After allot of tries I found the solution:
GameObject myButton;
myButton = GameObject.Find ("Button");
myButton.GetComponent().interactable = false;
myButton.GetComponent().interactable = true;
lot of tries? the answer was up there the whole time. your script makes no sense: "GetComponent()". Get what?
Your answer
Follow this Question
Related Questions
Working between different scenes! 1 Answer
Problem with a button 2 Answers
Unity UI Button not working 1 Answer
How to click a UI button without activating mouse 0 Answers
How is a Button component able to access a method on a prefab? 1 Answer