Im having some trouble getting into a Canvas object and changing the text inside the textbox with coding.
![]()
Im having some trouble getting into a Canvas object and changing the text inside the textbox with coding.
![]()
Just for clarification, is this the text input, where users can enter text?
Or is it a text field, where you display text?
I’m going to assume it’s the text field, since it sounds to be the closest to what you’re asking.
Simply
Text.GetComponent<Text>().text = "String you want to display.";
Text being the text field object.
Yep, it is a text field
Sorry, forgot to mention the script is outside. I tried something like this:
![]()
Im sorry, Im learnig to use paths but im not good yet ![]()
There are a few ways to improve it.
If this script is running on your canvas object, you could use
transform.GetChild[0].GetComponent<Text>().text = "Stuff";
If you want to use Find, then you could skip finding the canvas and go straight to finding Text.
Find("Text").GetComponent<Text>().text = "Stuff";
While the first is usually better than the second - when you have a lot of objects in the scene, Find can be quite intensive - the better method would be to define the reference in the editor
public GameObject textDisplay; //Put the text field in this spot in the editor
Update()
{
textDisplay.GetComponent<Text>().text += " hi."; //This will add " hi" to what is already in the text field every frame.
}
I forgot to mention, but you need to include UnityEngine.UI otherwise certain namespaces will not be recognized.
Ah! The issue was i didnt add the UnityEngine.UI, now it works
Thanks for your help!