Update text color according to player health

Hi, wanted to ask, how can i update player health text color, when player health is changing.
For example i’m doing something like this:

TextMeshProUGUI healthText = GetComponent<TextMeshProUGUI>(); ;
        int healthValue = gameSession.GetHealth();
        if(healthValue >= 300)
            // green
            healthText.color = new Color(4, 255, 29, 255);

Color remains the same. Is there something that i’ve missed ?

Hi Pasha,

Looksl like you need to use the Color32 class;

using UnityEngine;
using TMPro;

public class ColorChanger : MonoBehaviour
    public TextMeshProUGUI textMeshProGui;

    void Start()
        textMeshProGui.color = new Color32(4, 255, 29, 255);

See also;

Thanks for update, Color32 working fine. I didn’t know there will be a difference between Color and Color32.


You’re very welcome.

They are both structs, but the Colour32 appears to represent the data in 32 bit format., It is a fairly subtle difference between the two structs but if you hover over the color property of the textMeshProGui variable it will probably state in the intelligence what type it expects.

Hope this helps :slight_smile:

See also;

Privacy & Terms