TextMeshPro in Unity 2021.3.23f1 Health Display

Hi,
I started this course using Unity 2021 and if anyone would be wondering how to use TextMeshPro instead of Text in Health Display.
Hope doing it right.

3 Likes

Thank you for sharing this :pray:t3:

this is a solution but if you want to stay close to the tutorial then use what i have below. the important thing to remember with textmeshpro is there are two components you could get. the one you want is the one specifically for dealing with canvas. it is in the script below . this will keep you in line with the courses, so you dont have to go back and add serialized fields or refactor for enemies later( which is likely to be needed i dont know im not there yet).

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

namespace RPG.Attributes
{
public class HealthDisplay : MonoBehaviour
{
Health _health;

    private void Awake()
    {
        _health =  GameObject.FindWithTag("Player").GetComponent<Health>();
    }

    private void Update()
    {
        GetComponent<TextMeshProUGUI>().text = String.Format("{0:0}%", _health.GetPercentage());
    }
}

}

Privacy & Terms