BrandonFlynn-NB
BrandonFlynn-NB

Reputation: 400

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

Upvotes: 7

Views: 8297

Answers (3)

SK VARSHA
SK VARSHA

Reputation: 1

Go to Component->UI->Legacy->Text to insert textbox instead striving hard with TextMeshPro

Upvotes: -2

BrandonFlynn-NB
BrandonFlynn-NB

Reputation: 400

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

Upvotes: 15

Andreas W
Andreas W

Reputation: 275

Try using

textmeshPro = GetComponentInChildren<TextMeshPro>();

Upvotes: 2

Related Questions