Hello everyone,
I’m trying to have multiple lives for my player, 3 lives and I set this script it’s and now when I go down to 0 from 100 on my health it doesn’t destroy the object which is my player,
here’s my script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public GameObject GameManagerGo;
public GameObject playerExplo;
public float speed;
public float healthSpeed;
public Text healthText;
public Text LivesUIText;
public Image visualHealth;
private float currentValue;
public Canvas canvas;
public int currentLives = 3;
public int maxLives = 3;
public int minLives = 0;
public int currentHealth = 100;
public int maxHealth = 100;
public int minHealth = 0;
public GameObject PU_HealthSound;
public GameObject Expl;
public GameObject KillMeObject;
public int Health
{
get { return currentHealth; }
set
{
currentHealth = value;
}
}
void Start ()
{
currentLives = maxLives;
currentHealth = maxHealth;
}
void Update ()
{
HandleHealthbar();
currentLives = Mathf.Clamp (currentLives, minLives, maxLives);
currentHealth = Mathf.Clamp (currentHealth, minHealth, maxHealth);
if ((currentHealth == 0) && (currentLives == 0))
{
currentLives --;
LivesUIText.text = currentLives.ToString ();
Instantiate (playerExplo, transform.position, Quaternion.identity);
Destroy (gameObject);
}
{
KillMeObject.SendMessage ("_Dead");
}
}
void ApplyDamage(int damage)
{
currentHealth -= damage;
}
void IncreaseHealth ()
{
currentHealth += 25;
PU_HealthSound.GetComponent<AudioSource>().Play ();
}
private void HandleHealthbar()
{
healthText.text = "Health: " + currentHealth;
currentValue = Map(currentHealth, 0, maxHealth, 0, 1);
visualHealth.fillAmount = Mathf.Lerp(visualHealth.fillAmount, currentValue, Time.deltaTime* healthSpeed);
if (currentHealth > maxHealth / 2)
{
visualHealth.color = new Color32((byte)Map(currentHealth, maxHealth / 2, maxHealth, 255, 0), 255, 0, 255);
}
else
{
visualHealth.color = new Color32(255, (byte)Map(currentHealth, 0, maxHealth / 2, 0, 255), 0, 255);
}
}
public float Map(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
}