Skip to content

How to output text into UI

TextMeshPro (version without canvas)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
UnityEngine = require("UnityEngine")
TextMeshPro = require("TextMeshPro")

function Start()
    -- Bind the gameObject and use GetComponent
    local textGameObject = BoundObjects.textGameObject
    print(textGameObject)
    local textComponentFromGameObject = textGameObject.GetComponent("TMPro.TextMeshPro")
    print(textComponentFromGameObject)

    -- Change text
    textComponentFromGameObject.text = "TextMeshPro - Success"
end

TextMeshPro (version with canvas)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
UnityEngine = require("UnityEngine")
TextMeshPro = require("TextMeshPro")

function Start()
    -- Bind the gameObject and use GetComponent
    local textGameObject = BoundObjects.textGameObject
    print(textGameObject)
    local textComponentFromGameObject = textGameObject.GetComponent("TMPro.TextMeshProUGUI")
    print(textComponentFromGameObject)

    -- Change text
    textComponentFromGameObject.text = "TextMeshProUGUI - Success"
end
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
UnityEngine = require("UnityEngine")

function Start()
    -- Bind the gameObject and use GetComponent
    local textGameObject = BoundObjects.textGameObject
    print(textGameObject)
    local textComponentFromGameObject = textGameObject.GetComponent("UnityEngine.UI.Text")
    print(textComponentFromGameObject)

    -- Change text
    textComponentFromGameObject.text = "Text (Legacy) - Success"
end