- Home /
Can anyone identify the error with my GUITexture code?
Hello, I am trying to use GUITexture to indicate hearts for health for my player. I had it working temporarily, but must have changed or not saved something inadvertently. Here's the code I'm trying to use:
using UnityEngine;
public class HealthScript : MonoBehaviour
{
public int hp = 1; // Total hitpoints
public GUITexture HealthHeart1; //These symbolize the player's remaining hp on screen
public GUITexture HealthHeart2;
public GUITexture HealthHeart3;
void Start ()
{
if (this.gameObject.tag == "Player") {
HealthHeart1.enabled = false;
HealthHeart2.enabled = false;
HealthHeart3.enabled = false;
}
}
public void Damage(int damageCount)//Inflicts damage
{
hp -= damageCount;
if (hp <= 0 && this.gameObject.tag == "Player") { //player loses a life when hp reaches 0
CoinScript playerLives = this.GetComponent<CoinScript> ();
playerLives.UpdateLives (1);
} else if (hp == 1 && this.gameObject.tag == "Player") {
HealthHeart1.enabled = true;
} else if (hp == 2 && this.gameObject.tag == "Player") {
HealthHeart2.enabled = true;
} else if (hp == 3 && this.gameObject.tag == "Player") {
HealthHeart3.enabled = true;
}
The GUI textures are be enabled according to the appropriate hp, but instead they seem unaffected.
Comment
Best Answer
Answer by Danao · Oct 08, 2014 at 05:42 AM
Thanks for the reply. I got it to work by doing this in Update:
void Update() {
if (this.gameObject.tag == "Player") {
HealthHeart1.enabled = (hp == 1);
HealthHeart2.enabled = (hp == 2);
HealthHeart3.enabled = (hp == 3);
HealthHeart4.enabled = (hp == 4);
HealthHeart5.enabled = (hp == 5);
HealthHeart6.enabled = (hp == 6);
HealthHeart7.enabled = (hp == 7);
}