W
W
Wadim_wadim20002020-05-05 21:17:33
C++ / C#
Wadim_wadim2000, 2020-05-05 21:17:33

C# how to change text color at different float value?

I have a text in a unit called health. it was necessary to implement a change in its color with a different health indicator of the character. so that when it is more than 50 it becomes red, and when it is more than 80 it becomes red. I created the health variable in the script, and in the coroutine, under certain circumstances, increased it by 1 every 0.5 seconds. I wrote in the script

If(health >= 50)
{
obj.getcomponent<Text>().text.color = new color(и там три числа через запятую)
}
It works. But how to make it change color a second time if health is more than 80?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OblivionGM, 2020-05-05
@Wadim_wadim2000

For starters, instead of constantly calling GetComponent , use caching, because constantly calling GetComponent eats resources, which should be avoided.
And to specify more than one condition, but several, use
if (health >= 50 && health < 80) action
else if (health > 80) action
else action
The number of else if is not limited, but should not be abused.
The && operator means "and", read about them, which exist

A
Alexander Dyupachev, 2020-05-05
@inetti

If you need smooth transitions between colors, you can try using the Color.Lerp() function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question