代码之家  ›  专栏  ›  技术社区  ›  Ninja2k

TextMesh Pro文本不会通过脚本更改

  •  0
  • Ninja2k  · 技术社区  · 7 年前

    我似乎无法通过脚本更改TextMeshPro值。 在我的inspector中,我有一个名为Countdown的TextmeshPro对象。我有一个名为GameController的脚本,它附在这个脚本上。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    
    public class GameController : MonoBehaviour {
    
        public TextMeshProUGUI Countdown;
    
        // Use this for initialization
        void Start () {
    
            Countdown = GetComponent<TextMeshProUGUI> ();
            Countdown.text = "Hello";   
        }
    
        // Update is called once per frame
        void Update () {
    
        }
    }
    

    在inspector中有一个TextMesh字段,但由于某种原因,我无法将倒计时对象拖动到此字段,这可能是问题所在吗?

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   Technivorous    7 年前

    问题是,您使用的是常规TextMeshPro对象,而在代码中查找TextMeshProUGUI只是一个简单的错误。将代码更改为:

    public class GameController : MonoBehaviour {
    
        public TextMeshPro Countdown;
    
        // Use this for initialization
            void Start () {
        //you shouldnt need to get component the editor should take care of this for you when 
    //you drop it since you have the object set to TextMeshPro and not just GameObject
                Countdown = GetComponent<TextMeshPro> ();
                Countdown.text = "Hello";   
            }
    
            // Update is called once per frame
            void Update () {
    
            }
        }