- Home /
Error attempting to change text on instantiated button
Hi,
I've looked through multiple posts but can't seem to find out what I'm doing wrong. I've Instantiated a button prefab, added it to panel, set its scale, changed its name all fine. But when I attempt to change the text on the button I get the following error:
Assets/Scripts/gameSystem.cs(102,33): error CS0029: Cannot implicitly convert type UnityEngine.UI.Text[]' to UnityEngine.UI.Text'
The error points to the line "buttonText = newButton.GetComponentsInChildren();"
buttonDescription = objectDetail.objectTitle;
GameObject newButton = Instantiate(buttonPrefab) as GameObject;
Text buttonText;
buttonText = newButton.GetComponentsInChildren<Text>();
buttonText.text = buttonDescription;
newButton.transform.parent = buttonParent.transform;
newButton.transform.localScale = Vector3.one;
newButton.name = "objButton" + i;
Thanks
Im not use to that method, this is what I normally do for something like that, assu$$anonymous$$g you dont change the location of the text component added to the instantiated button.
newButton.GetComponent().text = buttonDescription;
Thats really the only method im aware of, and only method if ever used, so that should work for you. If not, the alternative would be:
newButton.transform.GetChild("Text").GetComponent().text = buttonDescription; //or: newButton.transform.FindChild(0).GetComponent().text = buttonDescription;
Answer by sas_88 · Jul 13, 2015 at 05:15 AM
Instead of create and assigning the text component.
Try to access it directly
newButton.GetComponentsInChildren().text=buttonDescription;
I don't know how many hours I wasted over that "s" in components. Thanks a lot for your help. $$anonymous$$uch appreciated.
Please flag the answer as accepted if it did indeed help you.
Your answer
Follow this Question
Related Questions
Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers
Multiple Prefab Instantiation, Camera, Button and Scroll View Question 1 Answer
An Instantiated UI Button doesn't work; clicking does nothing. 1 Answer
Buttons not showing up the first time the menu loads, but showing up on all menu openings after 2 Answers
Bearded Multi-Headed Instantiation Dragoon Question 1 Answer