title: Health bar sync with the health code script
body: <p>I create an UI image with the health bar and a script for it, but when my player is attacked, the image of the health bar don't change, just in the inspector the health shows the damage change. How can I make the UI image health bar to change when player is attacked? Do I need to change something in the script?</p>
  
  <pre class="lang-csharp prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
  
  
  public class HealthBar : MonoBehaviour
  {
      public static HealthBar singleton;
      public Image currentHealthbar;
      public Text ratioText;
  
      public float currentHealth;
      public float maxHealth = 100f;
      public bool isDead = false;
      public bool isGameOver = false;
      public GameObject gameOverText;
  
  
      private void Awake()
      {
          singleton = this;
      }
  
      // Start is called before the first frame update
      private void Start()
      {
          currentHealth = maxHealth;
          UpdateHealthbar();
      }
  
      // Update is called once per frame
      private void UpdateHealthbar()
      {
          float ratio = currentHealth/ maxHealth;
          currentHealthbar.rectTransform.localScale = new Vector3(ratio, 1, 1);
          ratioText.text = (ratio * 100).ToString("0") + '%' ;
      }
  
      void Update()
      {
          if(currentHealth &lt; 0)
          {
              currentHealth = 0;
          }
      }
  
      public void DamagePlayer(float damage)
      {
          if(currentHealth &gt; 0)
          {
              currentHealth -= damage;
          }
          else
          {
              Dead();
  
              if (isGameOver &amp;&amp; Input.GetMouseButtonDown(0))
              {
                  SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  
              }
          }
  
          UpdateHealthbar();
      }
  
      void Dead()
      {
          currentHealth = 0;
          isDead = true;
          Debug.Log("Player is dead");
  
          gameOverText.SetActive(true);
          isGameOver = true;
      }
  }
  </code></pre>