Ninja2k
Ninja2k

Reputation: 879

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

Upvotes: 3

Views: 19340

Answers (1)

Technivorous
Technivorous

Reputation: 1712

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.

Upvotes: 3

Related Questions