Question by
CrazyNinja2000 · Mar 05, 2016 at 01:24 PM ·
unity 5guitextguibutton
Changing Text within button
I am trying to set the text of a button via c# script. I have checked out other answers, but they do not help. I have tried this multiple times:
button.GetComponentInChildren<GUIText>().text = "Button";
But I always get the error "Object reference not set to an instance of an object". How can I change the text of the button?
Comment
Best Answer
Answer by CrazyNinja2000 · Mar 05, 2016 at 02:05 PM
Okay, so I found out that I needed to add "UnityEngine.UI".
Answer by Fydar · Mar 05, 2016 at 01:37 PM
If it is a button from the new Unity UI system, use:
using UnityEngine.UI;
//Later on in program
button.GetComponentInChildren<Text>().text = "Button";
Got the error "The type or namespace name 'Text' could not be found".
Your answer
