I have a piece of code that I am trying to use to display a number of hearts on the player’s screen as a health “bar”, the number of which being able to be changed (think Legend of Zelda).
I’ve written this code, which accepts my two textures (healthFull being the inside of the individual heart and healthEmpty being the frame in which it is inset). When the player loses health (say, from 10 health to 9), the tenth heart would just be the frame while the other 9 would be filled - See “LIFE”. The two textures are each 9 pixels by 8 pixels. I have attempted to place 3 pixels padding between each heart. My code(C#):
using UnityEngine;
using System.Collections;
public class PlayerHealthLF : MonoBehaviour {
public int maxHealth;
public int health;
public Texture2D healthFull;
public Texture2D healthEmpty;
// Use this for initialization
void Start () {
health = maxHealth;
}
// Update is called once per frame
void Update () {
TakeDamage(0);
}
// Initialize GUI
void OnGUI () {
for(int e = 0; e > maxHealth; e++){
GUI.DrawTexture(new Rect((9 * e) + 3, Screen.height - 11, 9, 8), healthEmpty);
}
for(int f = 0; f > health; f++){
GUI.DrawTexture(new Rect((9 * f) + 3, Screen.height - 11, 9, 8), healthFull);
}
}
// Use this to take damage
public void TakeDamage(int damage){
health -= damage;
}
}
…And nothing is displayed. Perhaps a more trained eye can reveal to me why exactly this isn’t working - a thousand thanks!