Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextMeshPro Null Reference Exception

EDIT: Solved. Used TextMeshProUGUI instead of TextMeshPro.

Background:
I'm using TextMeshPro to display text. I want to change the text via a script. The problem is I get a NullReferenceException when I attempt to change it.

Details:

My script:

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

public class UI_speed : MonoBehaviour {

    TextMeshPro textmeshPro;

    void Start () {
        textmeshPro = GetComponent<TextMeshPro>();
        textmeshPro.text = "test";
    }
}

The code matches the TextMeshPro API so I'm not sure what's going on. I'm using Unity 5.6.1f1. Any help is greatly appreciated. Thank you. My workstation

like image 522
BrandonFlynn-NB Avatar asked Dec 11 '17 21:12

BrandonFlynn-NB


2 Answers

Found a solution. Used TextMeshProUGUI instead of TextMeshPro. Unsure of the details but it works.

like image 143
BrandonFlynn-NB Avatar answered Oct 18 '22 11:10

BrandonFlynn-NB


Try using

textmeshPro = GetComponentInChildren<TextMeshPro>();
like image 40
Andreas W Avatar answered Oct 18 '22 11:10

Andreas W