- Home /
Question by
steveydunne · Apr 29, 2016 at 05:41 AM ·
uibutton2d gametextbutton trigger events
Clicking a button will display that buttons text in a seperate text-field
Hi guys, in case the title isn't clear, What I'm trying to do is this:
When I click on a button, the ChangeLabelToButtonsText() Method is called. Within the method, I want the labelObject's text to change to whatever the text on the button is.
Below is what I have but it's not working.
public GameObject labelObject; //labelObject is the GUI Text GameObject
void ChangeLabelToButtonsText()
{
labelObject.GetComponent<Text>().text = this.GetComponent<Text>().text;
}
Thanks guys,
Stephen
Comment
Answer by pfreese · Apr 30, 2016 at 03:05 AM
The Text component is actually on a child of the Button gameobject rather than the gameobject itself, so you'll need to find the component using something like:
labelObject.GetComponent<Text>().text = this.GetComponentInChildren<Text>().text;