简体   繁体   English

TextMeshPro文本不变,控制台提供快速错误

[英]TextMeshPro Text not changing, console is giving rapid errors


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++;
    }
}

The text is not changing, and I dont know why. 文字没有改变,我也不知道为什么。 The error in the console is "NullReferenceException: Object reference not set to an instance of an object Num.Update () (at Assets/Scripts/Num.cs:19)" 控制台中的错误是“NullReferenceException:对象引用未设置为对象Num.Update()的实例(在Assets / Scripts / Num.cs:19)”

The error is that your script isn't finding a TextMeshPro sibling component. 错误是您的脚本没有找到TextMeshPro兄弟组件。 If you're using the UI version, what you actually want is to find a TextMeshProUGUI 如果您正在使用UI版本,那么您真正想要的是找到TextMeshProUGUI

I'm guessing it's a text ui from what you said. 我猜这是你说的文字ui。 If it is a text UI and Textmeshpro, then use TMPro.TextMeshProUGUI varName; 如果是文本UI和Textmeshpro,则使用TMPro.TextMeshProUGUI varName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM