How to add hearts as I pick up healths?
I have a script that controls my players health. This means that if I fall off the map I lose a life and a Texture2D of a heart is reduced, you start with 3 hearts. I'm trying to whenever the player picks up a heart, its life increases, that part is working, but I can't seem to figure how to also increase the Texture2D. I've tried many ways, but nothing worked. I even tried reversing the damage part to see if it worked, but no, so I left it as it was.
Health Script:
#pragma strict
var lives = 3; //amount of health of the player
var livesTexture : Texture2D[]; //the texture for the health
var livesTextureSize = 30; //size for the health texture
function Update () {
if (lives == 0){
Application.LoadLevel("Scene_1");
}
}
function OnTriggerEnter (other : Collider) {
if (other.tag == "Enemy"){
if (lives-1 >= 0){
livesTexture[lives-1] = null;
lives -= 1;
}
}
if (other.tag == "Falling"){
if (lives-1 >= 0){
livesTexture[lives-1] = null;
lives -= 1;
}
}
if (other.tag == "Heart"){
if (lives+1 <= 3){
livesTexture[lives+1] = null; //I know it doesn't work, but it was worth I shot, I guess
lives += 1;
Destroy(other.gameObject);
}
}
}
function OnGUI () {
var livesTextureRect = Rect (10, 5, livesTextureSize, livesTextureSize);
for (var i : int = 0; i < lives; i++){
GUI.DrawTexture (livesTextureRect, livesTexture[i], ScaleMode.ScaleToFit, true, 0.0f);
livesTextureRect.x += livesTextureSize + 10;
}
}
Taken Damage: As you can see, the hearts (at the top left) was reduced and my life counter (at the Inspector) was alse reduced.
Restored Health: Here's the problem, the life counter (at inspector) was increased when I picked up the heart, but the hearts stayed at two (top left).
You only have two hearts elements inspector. did you try adding three?
Your answer
![](https://koobas.hobune.stream/wayback/20220612041047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set Active Random GameObject from array (JavaScript) 1 Answer
Jimmy vegas fps tutorial converting to c# 1 Answer
Unity AssetPreview.GetAssetPreview not compiling 0 Answers
Health Script 2 Answers