Changing color on an object based on health

I am trying to change the material color based on an object’s health points. I did create a Dynamic material instance, unfortunately the color wont change corresponding to the health of an object. the object is using an integer variable.

How many colours are we talking about? If it’s just 2, then you can just set the new material to the mesh instead of using the dynamic material instances. It’s expensive, to some degree.

3 colors, starting with 3 full health points, green yellow to red.


this screenshot is what i have so far, unfortunately it only displays yellow, if i set the health to “1” it remains black instead of red.

Your logic is wrong. I guess you have 3 health points (1 to 3).
If so, you need to see the condition if it satisfies it, then do what?

1 Like

while they do change in color, when i set the integer to whatever health i set it to, like 1 health shows up red and 3 shows up green. However it doesn’t change in real-time when the object loses health or in other words, the object stays green even when losing 1 point of health.

Yes, that’s because you use it in “Begin Play” event so it will only executes once at the start of the game. To do it in real time, you have to use functions or custom events so that you can trigger it during the gameplay.

So, for start, you could use debug keys to see how a key press event can trigger the function. But in real game, you should update this function upon damage received.

This image, can execute this function whenever you press 7 key on your keyboard.

1 Like

Okay turns out… I dint use the same parameter name as the vector3 name i had in the materials like an idiot, I fixed it accordingly and adjusted my coding as you showed me and everything is working as intended! thanks alot for the help I’m still fairly new with this.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.