- Home /
Destroy Player when Cur_Health <=0
I recently created a health bar and it "works" in one way at least, and it recognizes collisions and such and i want to destroy my Player when my current health(cur_health) is equal to zero. It seems simple but everything i try doesn't work. Also once my character is deleted how do i change scenes in game to my ending credits/Game Over? Below is my script if anyone can help me out it'd be greatly appreciated
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HealthScrpit : MonoBehaviour {
public Image healthbar;
public float max_health = 100f;
public float cur_health = 0f;
public bool alive = true;
void Start ()
{
alive = true;
cur_health = max_health;
SetHealthBar();
}
public void SetHealthBar()
{
float my_health = cur_health / max_health;
healthbar.transform.localScale = new Vector3(Mathf.Clamp(my_health, 0f, 1f), healthbar.transform.localScale.y, healthbar.transform.localScale.z);
}
public void TakeDamage(float amount)
{
if (!alive)
{
return;
}
if(cur_health <= 0)
{
cur_health = 0;
alive = false;
//gameObject.SetActive(false);
}
cur_health -= amount;
SetHealthBar();
}
// Update is called once per frame
void Update () {
}
public void TakeDamage(int damageAmount)
{
cur_health -= damageAmount;
if (cur_health <= 0)
Die();
Destroy(this.gameObject);
}
private void Die()
{
gameObject.SetActive(false);
}
}
Is HealthScrpit a component of your Player? Try using DestroyImmediate ins$$anonymous$$d of Destroy.
To change scenes, you use a Game$$anonymous$$anager, which is a script with "DontDestroyOnLoad", which runs in the background during your game. Generally, it's a singleton. Google "C# Singleton" if you don't know what they are.
Answer by Happeloy · May 14, 2018 at 05:48 PM
I can see your character is deleted every time you take damage. That's because you dont have any curly brackets on your if statement here:
cur_health -= damageAmount;
if (cur_health <= 0)
Die();
Destroy(this.gameObject);
It should be
cur_health -= damageAmount;
if (cur_health <= 0){
Die();
Destroy(this.gameObject);
}
Otherwise, only the line right after the if-statment is executed when the statement is true, the line after that is executed every time TakeDamage() is called.
Regarding changing you scene, you do that via the scenemanager, like this:
UnityEngine.SceneManagement.SceneManager.LoadScene("Scene name");
(Or simplify it by writing "using UnityEngine.SceneManagement;" at the top, and then just use "SceneManager.LoadScene("Scene name");"
And why do you have two different methods called TakeDamage(), one accepting a float, and one accepting an int? It is the one accepting an int that is causing your player to be destroyed.
I watch a tutorial and thats the script he wrote it works sort of, i tried using the brackets but the character is in game after cur_health reaches 0
I see. Well, if you watched a tutorial where they wrote this script, I suggest you find another tutorial because there are a few weird things going on here. First of all, remove this part completely:
public void TakeDamage(int damageAmount)
{
cur_health -= damageAmount;
if (cur_health <= 0)
Die();
Destroy(this.gameObject);
}
Since it doesn't make any sense, and if you say that the player isn't removed, it means that it's the other method that is called.
The reason why the player isn't removed, is because it isn't being removed. :) Try changing this part:
public void TakeDamage(float amount)
{
if (!alive)
{
return;
}
if(cur_health <= 0)
{
cur_health = 0;
alive = false;
//gameObject.SetActive(false);
}
cur_health -= amount;
SetHealthBar();
}
to this:
public void TakeDamage(float amount)
{
cur_health -= amount;
SetHealthBar();
if(cur_health <= 0)
{
Destroy(gameObject);
}
}
Now, this would remove the player completely when it dies. I don't know if that is what you want to do, but if you would like to change your scene after the player has died, you could add UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene("Scene name"); right after the Destroy(gameObject); Of course, you should change "Scene name", to the actual name of the scene you want to go to.
By the way, I removed the use of the alive bool, since it doesn't really do anything if you want to change scene as soon as the player dies. And I moved the subtraction of the health up, so it is executed before the check.
O$$anonymous$$G THAN$$anonymous$$ YOU IT WOR$$anonymous$$ED!!! HA HA lol omg that was so frustrating i tried so many things lmao thank you for helping me man! I really appreciate it! BTW do you happen to know how to make a health bar move from right to the left side like a regular health bar, i tried making the image in the UI a Filled image type, Fill $$anonymous$$ethod :Horizontal, Fill Origin: Left and it works when i mess with the Fill Amount but not in game
Your answer
Follow this Question
Related Questions
destroy game object (enemy) on game start instead of kill 2 Answers
Can someone explain the destroy () command? 1 Answer
Health bar depletes when app isn't open 1 Answer
how to make a healthbar increase 2 Answers
Health bar goes down instantly. 1 Answer