time.deltatime not working as intended

Hello,

In the game i work on there is a textmesh above character that shows its health. I want number in text to reduce one by one every sec when character takes damage but instead health text reduces to the current health of character all of a sudden like time.deltaTime has no effect on the function.

If you could help me on that i would appreciate it. Have a nice day.

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

public class HealthText : MonoBehaviour
{
public WheelFighter fighter;
public TextMeshProUGUI text;
// Start is called before the first frame update
void Start()
{
text.text = fighter.maxHealth.ToString();
}

// Update is called once per frame
void Update()
{
    int healthInText = int.Parse(text.text);

    if (healthInText > fighter.health)
    {
        healthInText = (int)(healthInText - 1 * Time.deltaTime);
        text.text = healthInText.ToString();
    }
}

}

When you cast it as int you remove all the decimals

healthInText = (int)(healthInText - 1 * Time.deltaTime);

For example 4.9995 cast as int becomes 4.


For example if we run your code with healthInText = 5 at the start
in the first update healthInText will be set to 4, due to being cast as int.
Second time we call update healthInText will be 4, and set to 3 being cast as int.
And so on.


Each update the healthInText is decreased by 1.
What you need is some kind of timer which keeps track of when to decrease healthInText