Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[Help required]How to make my healthbar appear upon hitting an enemy and despawn when enemy dies?

Discussion in 'Scripting' started by RPGcakes, Nov 12, 2018.

  1. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    I'm currently using a UIManager script to manage my enemy+player health bars and they work ok.

    The problem is that the healthbar is always on screen even before attacking an enemy and after the enemy dies usually stays on -1 at the top of the screen

    Code (CSharp):
    1. public class UIManager : MonoBehaviour {
    2.  
    3.     public Slider healthBar; //Slider Health Bar
    4.     public Text HPText; // Health Bar Text
    5.     public PlayerHealthManager playerHealth;
    6.  
    7.     public Slider enemyHealthBar;
    8.     public Text enemyHpText;
    9.     public EnemyHealthManager enemyHealth;
    10.  
    11.  
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.      
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         healthBar.maxValue = playerHealth.playerMaxHealth;
    23.         healthBar.value = playerHealth.playerCurrentHealth;
    24.         HPText.text = "HP: " + playerHealth.playerCurrentHealth + "/" + playerHealth.playerMaxHealth;
    25.  
    26.         enemyHealthBar.maxValue = enemyHealth.enemyMaxHealth;
    27.         enemyHealthBar.value = enemyHealth.enemyCurrentHealth;
    28.         enemyHpText.text = "Enemy Hp: " + enemyHealth.enemyCurrentHealth + "/" + enemyHealth.enemyMaxHealth;
    29. }
    30.  
    31.  
    Is there anyway to make the hp appear upon dealing damage with an enemy and disappear after the enemy dies until another enemy is attacked?

    Also when there is 2 enemies on screen it doesn't swap between the bars
     
  2. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    You should have a OnEnemyDead () sort of method on whatever script handles the combat mechanics or the enemy health. This function should be called when the enemy's health is zero or less. You could do this by using a getter/setter on your enemy health variable, with an if statement on the set mechanic. Let's say your enemy's health variable is simply called health, and is an integer. Then the getter/setter would be:

    Code (CSharp):
    1. private int Health {
    2.         get{
    3.             return health;
    4.         }set{
    5.             health = value;
    6.             if (health <= 0){
    7.                 OnEnemyDead();
    8.             }
    9.         }
    10.     }
    This would make it so that each time you modify the enemy health (using the Health getter/setter we've just defined) it would check whether his health is less or equal to zero, and act appropriately. On the OnEnemyDead() you should have a reference to the UI Manager deactivating the enemy health bar (perhaps

    enemyHealthBar.gameObject.SetActive(false);

    or something of this sort.

    Of course there are hundreds of approaches similar to this (and probably many better ones). You could simply check if eneyHealthBar.value is less or equal to zero on the Update() method; I didn't suggest that because I don't like using Update(). But that's a matter of taste :)
     
    RPGcakes likes this.
  3. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Ok I managed to make the enemy health bar disappear by doing

    Code (CSharp):
    1.   if (enemyHealthBar.value <= 0) enemyHealthBar.gameObject.SetActive(false);
    But now when I hit another enemy, the health bar is still disabled
     
    Last edited: Nov 12, 2018
  4. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    When you hit your enemy do the opposite;

    enemyHealthBar.gameObject.SetActive(true);

    and you're good to go.
     
  5. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Thanks for the help :)

    When I set
    Code (CSharp):
    1. if (enemyHealthBar.value <= 0) enemyHealthBar.gameObject.SetActive(false);
    when the enemy dies the bar disappears which is good, when I add the true statement the bar stays after an enemy dies (but without the slider)

    I tried setting
    Code (CSharp):
    1. if (enemyHealthBar.value >= 1) enemyHealthBar.gameObject.SetActive(true);
    under the above statement

    But this doesn't make the hp values reappear or the slider

    Is it because both if statements are in the same update?
     
  6. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Still unable to reference the code into the HurtEnemy script for the true statement, any ideas?
     
  7. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Wait, I feel like I need more information. HurtEnemy code? You didn't share that here. Share it! What do you mean you can't reference it? In what line of that code you are unable to have a reference?
     
  8. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Well when you mentioned hitting my enemy, I figured that you meant to put the 'EnemyHealthBar.gameObject.SetActive(true); command into the script that hurts the enemy, which is the HurtEnemy script

    Because when I use the UIManager that disables the healthbar with the false statement and then add the true statement underneath it doesn't remove the healthbar or reset it, it just removes the slider, I thought it was because there is 2 if statements under the update () but now I'm just really confused as to how to make the script work

    As for referencing, I just don't know how to reference other scripts, I tried a few examples of referencing scripts whilst searching the internet but I couldn't make them access the enemyHealthBar
     
  9. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Do the following. On your EnemyHurt script, add the following variable

    Code (CSharp):
    1. private UIManager uiManager;
    Then write the following code:

    Code (CSharp):
    1. void Awake () {
    2.   uiManager = FindObjectOfType<UIManager>();
    3. }
    This is how you reference other scripts. Let's go step by step. Firstly, you create a variable of type UIManager (because this is the class you want to access) and we name it uiManager. The name could be anything; it could be potatoes or mySuperCoolUIManager, but we are trying to make it clear and simple, so uiManager works fine.

    Then we use void Awake (). This is a Unity method that's very similar to Start (). I will not go into the details; sufficient is to say that this method is run when you start playing your game. The statemente

    uiManager = FindObjectOfType<UIManager>();

    is saying: my uiManager variable will store my object of type UIManager; in other words, it is the reference.

    This is how you reference another script. Now you can access your UIManager functions and variables (if they are public) through your uiManager variable.

    Now, I don't know how you detect when the enemy is being hurt; but when the enemy is being hurt, on the HurtEnemy script, you should say:

    uiManager.enemyHealthBar.gameObject.SetActive(true);

    Basically, this is saying (from the last to the first part of the statement):

    Set as active the game object named enemyHealthBar of my uiManager variable (which you assign, on the Awake() method, as your UIManager).

    If you have more doubt or problems, share the scripts involved; share the EnemyHurt script too. It's very hard to help when I can't see specifically how your code is being written.
     
    RPGcakes likes this.
  10. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Thanks for the detailed response! This was very informative :)

    I followed your instructions but upon killing an enemy the hp bar disappears(good) but upon hitting another enemy the bar won't reappear even with the new active script there (The enemyHealthBar just stays disabled in the hierarchy)
     
    Last edited: Nov 12, 2018
  11. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Again, please share your scripts. Also, write

    Debug.Log("Hitting");

    before setting the health bar back to true when you hit the enemy. Just to make sure that the code is being executed.
     
  12. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Debug is this

    Code (CSharp):
    1. Hitting
    2. UnityEngine.Debug:Log(Object)
    3. HurtEnemy:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/HurtEnemy.cs:40)
    4.  
    Ok I'll post my HurtEnemy code later today. I've been doing some testing with the script, I've managed to re enable the bar upon hitting an enemy, but it doesn't contain the slider, just an empty graphic.

    I think part of the issue is that the UIManager which links to EnemyHealthManager (Which sets the current and max value of hp) becomes unlinked when the enemy is killed. (Due to the gameObject being destroyed if current health is <0)

    For example (I'm using bushes as enemies at the moment)

    https://imgur.com/a/XHayMic

    if I manually click on the UI manager in the canvas groups and set another enemy (Small Bush 1) the bar reappears but without the values (The hp text/red bar) only the border of the hp bar reappears

    But when I half kill 'Small Bush' and then manually change the setting to 'Small Bush 1' and hit small Bush 1 it correctly changes hp with the new values on the correct enemy

    If I kill 'Small Bush' and then set 'Small Bush 1' manually it appears without the values

    Maybe I need some sort of targeting system or a way to switch between the 2 enemies?
     
    Last edited: Nov 13, 2018