- Home /
How to change button text via script?
I know this has been answered before but I have tried everything that other answers have given for the past few days without any success. I'm hoping someone can explain my fundamental misunderstanding behind this.
Here is the current iteration of my script. I've clicked and dragged a script into the Globals variable and I've clicked and dragged a button in to the ThisButton variable. The rest is obvious. I don't think I need the import UnityEngine.UI; but I've tried without it as well.
#pragma strict
import UnityEngine.UI;
var Globals : GlobalValues;
var ThisButton : UnityEngine.UI.Button;
var CrewMember = 0;
function Start () {
}
function Update () {
if (Globals.Crew1[0] == 1){
ThisButton.GetComponentsInChildren.<Text>.text = "testing";
}
}
Right now the error is that 'UnityEngine.Component.GetComponentsInChildren' is not a generic definition. But there have been plenty of other errors on other tries.
Basically, if my if statement is true, how do I change the text of my button? Thanks for any help on this.
Answer by allenallenallen · Aug 07, 2015 at 11:23 AM
Assuming you do have a Text component in one of the children of ThisButton, here's how you would fix it:
ThisButton.GetComponentInChildren(Text).text = "testing";
I recommend you to check the API: http://docs.unity3d.com/ScriptReference/GameObject.GetComponentInChildren.html
And make sure you're in JS instead of C#.
Thanks for the response.
To answer your troubleshooting points: In my hierarchy, the button does have a drop down arrow and there is a "Text" in the dropdown. It's my understanding that buttons are created with a Text child object. I am using javascript. I've read the GetComponentinChildren doc (and many others related to this problem) but haven't been able to solve why I can't get to the text component.
I changed the line to your suggestion (keeping in $$anonymous$$d the case sensitivity) and now I get the following error.
'text' is not a member of 'UnityEngine.Component[]'.
Again, thanks for your help.
Ignore my above comment, finally got a chance to look at it at home and monodevelop auto fills with "GetCompenentsInChildren" before "GetComponentInChildren" which was my problem. Once I swapped it to the singular version the above worked fine. Thanks again.
I needed to do GetComponentInChildren<Text>()
as it was giving me the error: "'Text' is a type, which is not valid in the given context".
Answer by Chromag · Jun 22, 2018 at 06:36 AM
The API is Component.GetComponentInChildren<>(); https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html
// instantiate button from prefab
GameObject go = (GameObject)Instantiate(Resources.Load("DefaultBtn"));
// get button component from game object
Button myBtn = go.GetComponent<Button>();
// get button text component in children and set the text property
myBtn.GetComponentInChildren<Text>().text = "my button text";
Your answer
Follow this Question
Related Questions
Rotating object with onscreen button 1 Answer
GUIText Problem With MENU 1 Answer
Gui Button Solid 2 Answers
How do I set a GUI button's text using a string from another script? 0 Answers
GUIText Help 1 Answer