The Unity 4.6 UI system has a component called Text . You can watch the video tutorial here . I also suggest that you check its API .
As always, you have two options for adding this component to the game object. You can do this from the editor (just click on the game object that you want to have this component in the hierarchy and add the Text component). Or you can do it with a script using gameObject.AddComponent<Text>() .
If you are not familiar with the components, I suggest you read the article.
Anyway, in your script you need to add using UnityEngine.UI; to the very top, because the Text class is in the UnityEngine.UI namespace. So now back to the script that sets the value to Text .
First you need a variable that refers to the Text component. This can be done by setting it to the editor:
public class MyClass : MonoBehaviour { public Text myText; public void SetText(string text) { myText.text = text; } }
And the binding of the gameObject with the text component to this value in the editor.
Another option:
public class MyClass : MonoBehaviour { public void SetText(string text) {
The previous script is not a good example, because GetComponent is actually expensive. Therefore, you may need to cache its link:
public class MyClass : MonoBehaviour { Text myText; public void SetText(string text) { if (myText == null) {
BTW, the "GetComponent" or "Add" parameter, if it does not already exist, is so common that usually in Unity you want to define a function
static public class MethodExtensionForMonoBehaviourTransform { static public T GetOrAddComponent<T> (this Component child) where T: Component { T result = child.GetComponent<T>(); if (result == null) { result = child.gameObject.AddComponent<T>(); } return result; } }
So you can use it like:
public class MyClass : MonoBehaviour { Text myText; public void SetText(string text) { if (myText == null) {