TextMeshPro 空引用异常

Bra*_*-NB 6 c# text unity5

编辑:解决了。使用TextMeshProUGUI代替TextMeshPro

背景:
我使用 TextMeshPro 来显示文本。我想通过脚本更改文本。问题是当我尝试更改它时会收到 NullReferenceException。

细节:

我的脚本:

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

代码与TextMeshPro API匹配,所以我不确定发生了什么。我正在使用 Unity 5.6.1f1。任何帮助是极大的赞赏。谢谢你。 我的工作站

Bra*_*-NB 9

找到了解决办法。使用TextMeshProUGUI代替TextMeshPro。不确定细节,但它有效。


And*_*s W 2

尝试使用

textmeshPro = GetComponentInChildren<TextMeshPro>();
Run Code Online (Sandbox Code Playgroud)