Question by
PeashyRedTail · May 11, 2018 at 02:45 AM ·
destroy objectdeathhealthbar
How to make character die when health is 0
I just wanted to ask how can i make my character die when health hits zero using my script here is the script i am using:
Health Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class YourHealth : MonoBehaviour {
Image healthBar;
float maxHealth = 1000f;
public static float health;
// Update is called once per frame
void Start () {
healthBar = GetComponent<Image> ();
health = maxHealth;
}
void Update () {
healthBar.fillAmount = health / maxHealth;
if
}
}
Damage Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health1 : MonoBehaviour {
void OnTriggerEnter2D(Collider2D col)
{
Health.health -= 10f;
}
}
Any help would be appreciated and have a great day
Comment
Answer by tormentoarmagedoom · May 11, 2018 at 07:49 AM
Good day.
What you mean for "die" ? what happens once life reaches 0 is up to you. What i can tell you is how to detect that.
at Health script, inside the Update() create a if sentence that checks if life has reached 0, like this:
void Update ()
{
healthBar.fillAmount = health / maxHealth;
if (health <= 0)
{
Debug.Log("Player is Dead");
}
}
So every frame will be checked, and the frame the health is under or equal 0, the code inside that if will be eexecuted.
Bye ;:D
Your answer
![](https://koobas.hobune.stream/wayback/20220612162428im_/https://answers.unity.com/themes/thub/images/avi.jpg)