Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jaguilar0047 · May 14, 2018 at 05:38 PM · destroydeletehealthbarhealth

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);
 

 }

   
 

   

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iJuan · May 14, 2018 at 05:47 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jaguilar0047 · May 14, 2018 at 06:01 PM 0
Share

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

avatar image Happeloy jaguilar0047 · May 14, 2018 at 06:33 PM 1
Share

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.

avatar image jaguilar0047 Happeloy · May 15, 2018 at 04:24 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges