Home > Software engineering >  TMPro is a namespace but is used like a type
TMPro is a namespace but is used like a type

Time:08-15

using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour {

    public Transform player;
    public TMPro scoreText;

    // Update is called once per frame
    void Update () {
        Debug.Log(player.position.z);
        scoreText.text = player.position.z.ToString();
    }
}

CodePudding user response:

You have to use the class inside this namespace. Use: public TMPro.TextMeshPro scoreText; Or public TextMeshPro scoreText;

if you have already added the namespace.

CodePudding user response:

Use TMPro.TMP_Text for your field type.

public TMPro.TMP_Text scoreText;

It's a base class for all TMP text components. BTW I'd make an extension for it in case it's null


public static class TextUtility
{
    public static void SetText(this TMPro.TMP_Text text, string value)
    {
        if (text)
            text.text = value;
    }
}

So you could do a myText?.SetText("some text"); and if it is null or destroyed this wouldn't raise an error.

  • Related