Health Decreasing via GUITexture

I have 4 gui.textures that expressing a heart fading. heart4.png with 100% opacity then heart3.png with 80% opacity, so on and so forth.

I want to use it as my health and decrease it when I stay in my water object which is a cube with interval of 5 secs.

How can I do that? Thanks!

First, you’ll need to determine when the player is and is not in the ‘water cube’ (maybe you’ve already got this part in place).

Then, you’ll need a timer (there are probably some other methods you could use here, but I’ll describe a timer-based method). The timer can just be a float variable associated with your script.

Each update, if the player is not in the water, reset the timer to zero. If the player is in the water, add Time.deltaTime to the timer; then, if the timer value is > 5, adjust the ‘health count’, and then reset/adjust the timer as follows (assuming you have the % operator available - you may have to adjust this if you’re using UnityScript):

timer %= 5f;

You can then choose which texture to render based on the current health count. An easy approach would be to store the textures in an array, which you would then index using the health count.

Anyway, I may not have gotten every detail there, but that should at least get you started.