How to get healthbar to increase and decrease with player health?

I’m working with a team to create a rough like game using Unity and C#. My job is to make the health bar for the player. I got the health bar to display, but I can’t seem to get it to communicate with my teammates player’s health system. Here’s my code:

      using UnityEngine;
      using UnityEngine.UI;
    using System.Collections;

  public class MR_Health : MonoBehaviour
 public int startingHealth = 100;                            // The amount of health the player starts the game 
   public int currentHealth;                                  // The current health the player has.
  public Slider Slider;                                 // Reference to the UI's health bar.
        public Image damageImage;                                   // Reference to an image to flash on the screen 
      being hurt.
      public AudioClip deathClip;                                 // The audio clip to play when the player dies.
    public float flashSpeed = 5f;                               // The speed the damageImage will fade at.             
   public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     // The colour the damageImage is set to, to 

Animator anim;                                              // Reference to the Animator component.
AudioSource playerAudio;                                    // Reference to the AudioSource component.
LH_Health playerMovement;                              // Reference to the player's movement.

LH_Health playerShooting; // Reference to the PlayerShooting script.

LH_Health playerHealth;

bool isDead;                                                // Whether the player is dead.
bool damaged;                                               // True when the player gets damaged.

void Awake()
    // Setting up the references.
    anim = GetComponent<Animator>();
    playerAudio = GetComponent<AudioSource>();
    playerMovement = GetComponent<LH_Health>();
    playerShooting = GetComponentInChildren<LH_Health>();
    playerHealth = GetComponent<LH_Health>();

    anim = GetComponent<Animator>();
    // Set the initial health of the player.
    currentHealth = startingHealth;

void Update()
    // If the player has just been damaged...
    if (damaged)
        // ... set the colour of the damageImage to the flash colour.
        damageImage.color = flashColour;
    // Otherwise...
        // ... transition the colour back to clear.
      //  damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);

    // Reset the damaged flag.
    damaged = false;

public void TakeDamage(int amount)
    // Set the damaged flag so the screen will flash.
    damaged = true;

    // Reduce the current health by the damage amount.
    currentHealth -= amount;

    // Set the health bar's value to the current health.
    Slider.value = currentHealth;

    // Play the hurt sound effect.

    // If the player has lost all it's health and the death flag hasn't been set yet...
    if (currentHealth <= 0 && !isDead)
        // ... it should die.

void Death()
    // Set the death flag so this function won't be called again.
    isDead = true;

    // Turn off any remaining shooting effects.

    // Tell the animator that the player is dead.

    // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing).
    playerAudio.clip = deathClip;

    // Turn off the movement and shooting scripts.
    playerMovement.enabled = false;
    playerShooting.enabled = false;


My teammate’s health system function is LH_Health. GetHp is what he uses to calculate the player health. I have my slider set as the Health bar is the inspector, so I’m not sure why it’s not working

It really seems odd to split this into multiple scripts. The health system should handle all things health related. Taking damage, death, re-spawn, as well as update the UI to reflect those changes.

So without the code from the LH_Health script I can’t tell you everything that might be wrong here.
Though I see a few logical problems I can point out that might help.

  1. LH_Health script is not enabled in your inspector screenshot.
  2. You call GetHP() from that script but you aren’t really doing anything with it.? Again I’m not sure what this function does exactly.
  3. You are only setting a slider value on damage taken calls. So unless you call take damage you never change the slider. (which i don’t see you do anywhere here)
  4. Why have you written a take damage call at all? Again if the health system is already written. You shouldn’t even need this code.

In theory to add in health bar updates to a functional health system script. You should just need to update the slider value to the health value of the script provided. That might be what the GetHP() function does. So slider.value = GetHP(); in a update call should sync whenever his health script updates the health value.

To do the flashing you really need to write this in the actual take damage function. Again I can’t figure out why this would be separated from the rest of the health system. So likely this is in the other script already. So your function is never called.

Seeing all the references to his script at the top. I’m pretty sure you should just be modifying your own copy of his script. Once you have the added functionality you have been asked to add, submit it back to them. If everything works they will commit the changes. Seems like your just on the wrong idea you need to write your own script to add these functionalities.

Hope this helps somehow. Working within a team can get chaotic at times.

You have … public Slider Slider;

As opposed to naming the variable you’ve declared it twice? Very new coder here so I could be talking out my a#%

i want to now how i can make the slider move if the player hits spike or sow