How to dynamically create an UI text Object in Unity 5?

I have been searching for something that should be simple, but Unity 5's documentation on that is fairly outdated. What I want to achieve is to be able to create UI Text totally from script, i.e. fully dynamically - in my real case application, I will do that in a loop of not pre defined number of iterations.

A search using Google will find quite many examples on how to do that, but all I saw either use methods that are already deprecated (are from before Unity version 5) or are simply wrong (no surprise here...). I already know that I should first add a Canvas to my project, then I should include using UnityEngine.UI in my C# code, and also that I could declare a UI Text like Text _guitext.

However, the code below does not work. I mean, it's not that it crashes, but rather that nothing is shown:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MyClass: MonoBehaviour {

Text _guitext;

// Use this for initialization
void Start () {
_guitext.text = "testing";
}

// Update is called once per frame
void Update () {
}
}


Worse than that, it seems that while game is played, no new object appears in the object hierarchy list.

Could you please point me out in the right direction here? Thanks.

3 Answers

I think what you are looking for is something like following:

GameObject CreateText(Transform canvas_transform, float x, float y, string text_to_print, int font_size, Color text_color)
{
GameObject UItextGO = new GameObject("Text2");
UItextGO.transform.SetParent(canvas_transform);

RectTransform trans = UItextGO.AddComponent<RectTransform>();
trans.anchoredPosition = new Vector2(x, y);

Text text = UItextGO.AddComponent<Text>();
text.text = text_to_print;
text.fontSize = font_size;
text.color = text_color;

return UItextGO;
}


It means, you first create an Empty GameObject. Then you add a Text component, with the specific local and/or anchored parameters, set the desired text and its color, then you make this object a child of the empty game object.

You want to retrieve the UI Text information from or set variable of that object you've created via script, you can then just use yourGOname.GetComponent<Text>().text, yourGOname.GetComponent<Text>().font, etc.

And of course, for that to work you need to be using UnityEngine.UI.

• Your example script throws an error for 'Text' in Text text, fontsize, and color. Text' does not contain a definition for text' and no extension method text' of type Text' could be found (are you missing a using directive or an assembly reference?) – Ian S May 8 '16 at 18:40
• @IanSteffy are you sure it isn't because you forgot to follow the last line of my answer, i.e. to include the using UnityEngine.UI at the beginning of the script? – MAnd May 11 '16 at 7:49

If you are looking for a quick&dirty solution for debugging, you can still use the old Unity 4 solution. Implement the OnGUI event of any MonoBehavior you have always active to call the GUI class to add some UI element to the current rendering frame.

void OnGUI() {
GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
}


If you want to use the canvas, you need to add a script to the canvas which:

1. creates a new GUIText game object
2. sets the text on that object
3. appends it as a child to the transform of the canvas

1 do you mean you want change the uitext dynamically?

If you want to change it, first u have to attach your script to gameobject. Then bind your variables. Tutorials could be found here

 transform.gameObject.GetComponent<Text>().text = "123";


2 If you want to create uitext which is not in Hierarchy beforehand.

You could use GUI. GUI Label

Put the created instances in OnGUI. It is like the update which is called every frame.

3 Actually 2nd answer does not create uitext object.

But it will achieve the same results. If you really want the uitext object instead of the effects. U may do it in a prefab, and initiate it in runtime. However, the uitext should be child of canvas.

• Thanks for your answer. However, I really don't want to change, but to create the UI Text (no prefabs, real GUI Texts). I read that official reference, but there is no clue in it about how to create UI Texts just using scripts (i.e. without putting them in the editor). There is this reference here, but also very vague: docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html – Kim Shutter Feb 5 '16 at 9:03
• @KimShutter The link u referred mentioned > In order to be able to easily instantiate UI elements dynamically, the first step is to create a prefab for the type of UI element that you want to be able to instantiate – AlexWei Feb 5 '16 at 9:06