Home > Software design >  Error when referencing TextMeshPro in Script
Error when referencing TextMeshPro in Script

Time:04-26

I'm switching from Text(legacy) to TextMeshPro in Unity. When switching I've tried using

public TextMeshProUGUI ScoreText;

but get this error: Assets\scrips\gamemanager.cs(17,9): error CS0246: The type or namespace name 'TextMeshProUGUI' could not be found (are you missing a using directive or an assembly reference?).

Script:

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


public class gamemanager : MonoBehaviour
{   
    public TextMeshProUGUI ScoreText; 
    int scoreText = 0;
    string ScoreText_string;

    
    
    
    public void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex   1);
    }

    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void plusTen()
    {
        scoreText  = 10;
    }
    
    

    void Update() 
    {
        ScoreText_string = scoreText.ToString(); 
        ScoreText.text = ScoreText_string;


    }




}

Note: editor version 2021.3.1f

CodePudding user response:

TextMeshProUGUI class resides in TMPro namespace. You must have to use the namespace TMPro with using statement or simply do

public TMPro.TextMeshProUGUI ScoreText;

Also, you must have to import the TextMeshPro package in your project as well.

  • Related