- Home /
How to change the text of a Button with C# in 4.6
I've searched everywhere for this and I can't find an answer. I am instantiating a set of 17 different buttons, and I want to be able to edit the text field of those buttons using C# code.
for(int i = 1; i<7;i++)
{
for(int k=1; k<4; k++)
{
GameObject createdbutton = (GameObject)Instantiate(button);
createdbutton.transform.parent = this.gameObject.transform;
This successfully creates the buttons, but how do I now change the text field of each button, what is the correct command?
I am quite new to Unity and C# so please be very clear, thanks!
If Uldeim's answer solved your problem and there are no better answers, remember to accept it by pressing the checkmark below the vote up/down buttons of the question :) This is so that other people will see your question as "solved".
This is one of the best forums I have seen! The answer was given perfectly and in a not complicated way. Amazing Job! I love seeing this!
Answer by Uldeim · Nov 07, 2014 at 03:33 AM
The new controls in 4.6 are pretty cool, but they're also kind of weirdly designed. The Text element of all uGUI buttons is actually a separate, child element of the button itself. Try the following code:
createdButton.GetComponentsInChildren<Text>().text = "New Super Cool Button Text";
The reason for this weirdness is the graphics component. Internally the engine calls rendering once on each GameObject. Allowing multiple graphics components would make the engine less efficient. Since 99 percent of the time there is no need for this buttons get shafted. As a button requires two graphics components it must have two GameObjects.
Also worth checking you have using UnityEngine.UI at the top of your script
Answer by cashif · Oct 23, 2015 at 07:33 PM
I think it must not be GetComponentsInChildren, but GetComponentInChildren. "s" makes the difference.
GetComponentsInChildren() will return an Array, a T[] to be exact, containing all components of type T on all child objects.
GetComponentInChildren() will return single one (the first) component of type T that was found on any of the child objects.
Both work, which one to pick depends on the situation, in this case you only need/want 1, so "without s" would be slightly more appropriate.
Answer by sfebrigade · Jun 18, 2016 at 05:46 AM
createdButton.GetComponentsInChildren().text = "New Super Cool Button Text";
This doesn't work. GetComponent() has no defintion for "text".
Is GetComponentInChildren in Singular. I guess the above example returns a list of Text components
Try either...
createdButton.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text"
...to access the text of the first element of the Text[] (Array of Text-Components) returned by GetComponent*s*, or...
createdButton.GetComponentInChildren<Text>().text
The type parameter is required to make it work either way, and the script you place it all in needs to be aware of the Text class in the first place, i.e. either import the UI namespace...
using UnityEngine.UI;
...or (not recommended) specify it when you use the Text...
GetComponentsInChildren<UnityEngine.UI.Text>().text
That should fix it.
Answer by FalsAlarm · Apr 21, 2017 at 08:24 AM
I think the best way to handle this situation is instead to keep an array of your button objects. First create an empty array of the same type as your Button.
Then, in your loop when you create the buttons, you might want to just push the button onto the end of the array right after you create it.
then, later on, in your event handler, or in the update function you could iterate over your button objects and set the .text property on them to be whatever you want(assuming you want them all to be the same text, then this will work fine).
However, if you wanted each button to have it's own text, then it could still be done. back when you pushed the item onto the array you could create some kind of unique identifier variable, perhaps the variable could be called realName. you could assign each button a realName and then later use the variable to identify it. @EsoEs
Your answer
![](https://koobas.hobune.stream/wayback/20220613170922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
v4.6 Create GUI Elements Via Script? 1 Answer
Text wobbles as Content changes 2 Answers
Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers