Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextMesh Pro text will not change via script

I cannot seem to change my TextMeshPro value via script. In my inspector I have a TextmeshPro object named Countdown. I have a script named GameController which is attached to this.

My script then sets the string value of Countdown to Hello but it does not work.

GameController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class GameController : MonoBehaviour {

    public TextMeshProUGUI Countdown;

    // Use this for initialization
    void Start () {

        Countdown = GetComponent<TextMeshProUGUI> ();
        Countdown.text = "Hello";   
    }

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

    }
}

In the inspector there is a field for TextMesh but I cannot drag the CountDown object to this for some reason, could that be the issue?

enter image description here

like image 520
Ninja2k Avatar asked Nov 18 '25 10:11

Ninja2k


1 Answers

the problem is that you are using a regular TextMeshPro object, and in your code your looking for a TextMeshProUGUI, simple mistake. change code to:

public class GameController : MonoBehaviour {

    public TextMeshPro Countdown;

    // Use this for initialization
        void Start () {
    //you shouldnt need to get component the editor should take care of this for you when 
//you drop it since you have the object set to TextMeshPro and not just GameObject
            Countdown = GetComponent<TextMeshPro> ();
            Countdown.text = "Hello";   
        }

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

        }
    }

the only way to make a TextMeshProUGUI object is to add it through a canvas. in your scene when you just add a TMP it will be Regular TMP which your "countdown" is. you can tell because it uses the TMP script not the TMPUGUI script.

like image 109
Technivorous Avatar answered Nov 20 '25 22:11

Technivorous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!