[C# Scripting] - Alter the button text

Hi

I have that code and I need alter the button text. How could I make it?

for(int i = 0; i < 5; i++)
{
  GameObject newButton = Instantiate(prefab);
  newButton.transform.SetParent(this.transform, false);
  newButton.GetComponent<Image>().sprite = mySprite;
}

Thank Toy

There are several ways of doing it. But if you only have a single text component in a child, just use getcomponentinchildren

GetComponentInChildren().text = “This is text”;

The main point is you have to get the component in the child.

ok. That is it correct ?

GameObject newButton = Instantiate(prefab);
newButton.transform.SetParent(this.transform, false);
newButton.GetComponent<Image>().sprite = mySprite;
newButton.GetComponentInChildren<Text>(this).Text="My Text";

Thank You

There are a few things wrong with it!

  1. .Text should be lowercase ‘.text’
  2. GetComponentInChildren does not need a parameter in the parenthesis → ‘(this)’ should be ‘()’.

Hi, ok.

Is it correct?

GameObject newButton.GetComponentInChildren<Text> ().text = "Teste";

Thank You