首页 > 解决方案 > TextMeshPro 文本没有变化,控制台出现快速错误

问题描述


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

public class Num : MonoBehaviour
{
    private int score;
    public TextMeshPro TMP;

    void Start()
    {
        TMP = GetComponent<TextMeshPro>();
        score = 0;
    }

    void Update()
    {
        TMP.text = score.ToString();
        score++;
    }
}

文字没有改变,我不知道为什么。控制台中的错误是“NullReferenceException:对象引用未设置为对象 Num.Update () 的实例(在 Assets/Scripts/Num.cs:19)”

标签: c#unity3d

解决方案


错误是您的脚本没有找到TextMeshPro同级组件。如果您使用的是 UI 版本,那么您真正想要的是找到一个TextMeshProUGUI


推荐阅读