Cannot implicitly convert type 'string' to 'TMPro.TextMeshProUGUI'

Hello!
I know this is a super-simple and noob question but I am having some problems with a simple task:

public TextMeshProUGUI test_;

    private void Start()
    {
        int number = 10;
        test_ = "How stressed am I? " + number;
        test_ = "How stressed am I? " + number.ToString();

    }

I “Cannot implicitly convert type ‘string’ to ‘TMPro.TextMeshProUGUI’”

How does it works please?

Thank you and sorry for my noobness

Well, TextMeshProUGUI is a component. It’s not a string. You need to set its text with either the .text property, or the .SetText() method.

4 Likes

:(:(:frowning:
I swear I feel dumb. I ALWAYS used this and this morning i missed that “.text =”.

Thank you so much! :sweat_smile:

1 Like

for anyone looking for an answer you need to add .text as you are changing the text value of the component not the component itself.

Ex

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class HealthBar : MonoBehaviour
{
    public Slider slider;
    public TMP_Text text_HealthValue;

    public void SetMaxHealth(float maxHealth)
    {
        slider.maxValue = maxHealth;
        slider.value = maxHealth;
        text_HealthValue.text = maxHealth.ToString();
    }

    public void SetHealth(float health)
    {
        slider.value = health;
        text_HealthValue.text = health.ToString();
    }
}
1 Like