how do i reference to a text objects text in scripting?
c# script
referencing the script to the number within the text object.
This is for an OnMouseDown script.
BTW The screenshot is useless. We all know what a Text component looks like.
Answer by Derpybunneh · Jul 17, 2016 at 03:03 PM
If your text and script are different objects, in the script make to sure add a using UnityEngine.UI
at the top so that you'll be able to edit the text.
Then make a public Text textObject
. Then from within the editor you can drag the text object onto the slot.
Then you can just use textObject.text = "whatever text you want";
Or you could have the script search for the text object using GameObject.Find()
.
How you could do this is: Text textObject = GameObject.Find("textObject");
. Put it in the Start() function.
More on GameObject.Find()
can be found here.
Answer by Eudaimonium · Jul 16, 2016 at 03:42 PM
If both text and your script are on the same gameobject,
gameObject.GetComponent<Text>().text = "Your string here, yo!";
So will this refer to the text within the text component?