- Home /
UI Button-parameters inaccessable in spite of "using UnityEngine.UI"
I am trying to access any parameters on a UI-Button, but they are being highlighted red in the script and I get Errormessages altough I have "using UnityEngine.UI;" implemented in my script. Example Error: "...Type Button' does not contain a definition for
interactable' and no extension method interactable' of type
Button' could be found (are you missing a using directive or an assembly reference?)"
My script simplified:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class MyScript: MonoBehaviour { public Transform tButton;
void SetButtons() {
if(blabla)
{ Transform newButton = Instantiate(tButton, tButton.transform.position + v3NewButtonPosiOffset(), Quaternion.identity) as Transform; //some buttons will be instantiated aside each other
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ButtonScript : $$anonymous$$onoBehaviour {
public Transform tButton;
void Start()
{
SetButtons();
}
void SetButtons() {
Transform newButton = Instantiate(tButton, tButton.transform.position , Quaternion.identity)
as Transform;
newButton.GetComponent<Button>().interactable = false;
}
}
@Chioni It is working fine. you have some other issue.
I copied your script and it still claims it cannot find the method. What else could be wrong? I also tried reinstalling Unity. Is it something to do with my Windows?
@Chioni $$anonymous$$ake sure you must have Unity version 4.6 or above
@Chioni Try exiting Unity and Visual Studio, then running it again.
I copied your script. This shows it cannot find the method. So what else could be wrong?
Oh and it's not the scripts name (I was too fast and forgot to rename it, but it still doesn't work).
Answer by Chioni · Jun 06, 2016 at 09:35 PM
Apparently it was a problem with my Unity 5.1.0 version (whyever). In 5.3.5 using UnityEngine.UI works just fine and Button-methods are accessable.
Answer by Rotavele · Jun 03, 2016 at 02:21 PM
First I'd make sure you have the button prefab/game object set up and that it is instantiating that. From what I see you're only instantiating a transform.
It's just a simplified pseudo-script. $$anonymous$$y real script works fine if I leave out any lines which try to access Button()-something methods e.g. -.interactable or anything else from here https://docs.unity3d.com/ScriptReference/UI.Button.html
Btw "only instantiating a transform" why only? it's exactly what should happen; I am duplicating a public transform which is inserted in the editor. You can do that with either gameObject or Transform, it doesn't matter which one, just try it^^
Thank you, hadn't realised I had another script called button in my game.
Your answer
Follow this Question
Related Questions
Only want to detect button click event 1 Answer
How to keep your button selected after clicking away ??? 1 Answer
UIButton highlighted color not working 0 Answers
TOGGLE SCRIPT 1 Answer
how to test Buttons 1 Answer