Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Enemy Health bars help needed

Discussion in 'Scripting' started by FirstBB8Droid, Jun 1, 2020.

  1. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I'm trying to add health bars to my enemies and I have some code written to do so, it isn't working, I just need help to figure out how to get this working
    here's the code
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. public class Target : MonoBehaviour{
    5.  
    6.     public float health = 50f;
    7.     public Slider slider;
    8.    
    9.  
    10.     void Start()
    11.     {
    12.         slider.value = health;
    13.     }
    14.  
    15.     void update()
    16.     {
    17.         slider.value = health;
    18.     }
    19.     public void TakeDamage (float ammount)
    20.     {
    21.         health -= ammount;
    22.         if(health <= 0f)
    23.         {
    24.             Die();
    25.         }
    26.         void Die()
    27.         {
    28.             Destroy(gameObject);
    29.            
    30.         }
    31.     }
    32.  
    33. }
    34.  
    any help will be greatly appreciated and if you need any other code just tell me.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    For starters,
    update()
    needs to be
    Update()
    . Capitalization is important.
     
    FirstBB8Droid likes this.
  3. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    Thanks man That was my problem and now I fell really stupid. Thanks, this helped me a lot.