Text.text

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public string text;

Description

The string value this Text displays.

Use this to access or edit the message displayed in Text. Use other Text properties such as size, font, and alignment to change the appearance of the text. These properties modify the text in this example:

using UnityEngine;
using UnityEngine.UI;

// UI.Text.text example // // A Space keypress changes the message shown on the screen. // Two messages are used. // // Inside Awake a Canvas and Text are created.

public class Example : MonoBehaviour { private enum UpDown { Down = -1, Start = 0, Up = 1 }; private Text text; private UpDown textChanged = UpDown.Start;

void Awake() { // Load the Arial font from the Unity Resources folder. Font arial; arial = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");

// Create Canvas GameObject. GameObject canvasGO = new GameObject(); canvasGO.name = "Canvas"; canvasGO.AddComponent<Canvas>(); canvasGO.AddComponent<CanvasScaler>(); canvasGO.AddComponent<GraphicRaycaster>();

// Get canvas from the GameObject. Canvas canvas; canvas = canvasGO.GetComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay;

// Create the Text GameObject. GameObject textGO = new GameObject(); textGO.transform.parent = canvasGO.transform; textGO.AddComponent<Text>();

// Set Text component properties. text = textGO.GetComponent<Text>(); text.font = arial; text.text = "Press space key"; text.fontSize = 48; text.alignment = TextAnchor.MiddleCenter;

// Provide Text position and size using RectTransform. RectTransform rectTransform; rectTransform = text.GetComponent<RectTransform>(); rectTransform.localPosition = new Vector3(0, 0, 0); rectTransform.sizeDelta = new Vector2(600, 200); }

void Update() { // Press the space key to change the Text message. if (Input.GetKeyDown(KeyCode.Space)) { if (textChanged != UpDown.Down) { text.text = "Text changed"; textChanged = UpDown.Down; } else { text.text = "Text changed back"; textChanged = UpDown.Up; } } } }

Did you find this page useful? Please give it a rating: