- Home /
NEW GUI - Button - set interactable via script
I'm trying to recreate the cool down of a button after been used, so I have a timer and all but my problem is that when I try this:
used.GetComponent<Button> ().interactable = true;
it doesn't recognize the variable/bool interactable and yes I'm using Unity Engine.UI at top, and when I try to type it, those are my options:
does is called something else or what should it be??
forgot to tell if really matters, also try Gameobject used ins$$anonymous$$d of Transform
As far as I know the thing you tried...
used.GetComponent<Button>().interactable = true;
Is the correct code.
If you try this, does it compile? (Do you get an error in the console log in Unity that persists after you clear it?)
If you don't get an error, your problem is that for some reason your autocomplete isn't working, did you try restarting $$anonymous$$onoDevelop/visual studio? :D
yes I got an error in the unity console it says:
"Type Button does not contain a definition for 'Interactable' and no extension method 'Interactable' 'Button' could be found"
See if you have another class named "Button" in your project. Probably also a $$anonymous$$onoBehaviour, or your GetComponent would give you errors first. When you're sure that a class contains a certain method or property, and the compiler says it doesn't, often it's because the compiler is seeing another class with the same name and trying to use that one.
Also, the variable in question is called "interactable" (no capital letter) and not "Interactable", so that error the compiler gave you is actually correct :)
Answer by mazket · Dec 06, 2014 at 06:35 AM
CanisLupus was rigth on the money for some reason I create a script called button and that was where the getComponent was looking for and couldn't find interactable, delete it and worked, Thanks for the comments and answer!!
Answer by Kiwasi · Dec 06, 2014 at 12:52 AM
Sounds like its a conflict. Button.interactable is the correct member to access. Several users have reported conflicts with some plugins.
Two options to resolve.
Find the conflict and rename the class. Right click on Button in your code editor. Go to "Find Declaration".
Use the fully qualified name. As in
GetComponent()
Just to confirm the other readers. I still had errors using GetComponent().interactable but Bored$$anonymous$$ormon's suggestion of using GetComponent() works.
Answer by jchart7 · Nov 02, 2015 at 02:46 PM
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
Answer by M-G-Production · Dec 05, 2014 at 10:18 PM
Hi mazket!
Are you trying to acces a Script Component named "Button" or an GameObject?
If Button is an object:
using UnityEngine;
using System.Collections;
public class Cooldown : MonoBehaviour {
public GameObject used;
// Use this for initialization
void startCD ()
{
StartCoroutine (SkillCD ());
}
// Update is called once per frame
IEnumerator SkillCD ()
{
yield return new WaitForSeconds(10f);
used = GameObject.FindGameObjectWithTag ("Used");
used.GetComponent<Button> ().interactable = true;
used.tag = "Untagged";
}
}
QUESTION 1: Is "Button" an object or a script name? If it is an object, change the "Button" to the actual script name you are trying to access.
Is "interactable" a private bool? If it is private, it won't be accessible! Switch to public!
I think you are mixing some components...
Bests, Math!
well what I'm trying to access is the the script called button that is attached to the GameObject button( the new UI system of unity), like this:
SideNote : This happens when I open the project from my external Hardrive (where is saved), but If I'm on a different computer without my project (external Hardrive) and start a new project with a button and a script (with the same content) monodevelop recognize Interactable.
This could be name conflict as CanisLupus said! or simply resources mess... If the button is on the PC but not in the Asset's folder in the external drive, it's going to be a mess...
I suggest you to try putting your code in an Update() fonction ins$$anonymous$$d and to program a Timer... Don't forget to hit like if I helped you, no one gives me likes :'(
Anyway, good luck! $$anonymous$$ath
I know you mean well $$anonymous$$ath, but if you want upvotes, try to answer questions only if you have good knowledge about the topic. If you don't understand the problem but try to answer it anyway, you'll inevitably end up giving unhelpful or irrelevant information (or asking the asker a lot of unnecessary questions).
Best of luck :)
Answer by jhrhee · Mar 17, 2015 at 03:54 PM
You should use UnityEngine.UI.Button instead of Button than all problem will be solved.
Your answer
