Advertisement
evelynshilosky

HealthBar - Part 31

Feb 2nd, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class HealthBar : MonoBehaviour
  7. {
  8.  
  9.     private Slider slider;
  10.     public Text healthCounter;
  11.  
  12.     public GameObject playerState;
  13.  
  14.     private float currentHealth, maxHealth;
  15.    
  16.     void Awake()
  17.     {
  18.         slider = GetComponent<Slider>();
  19.     }
  20.  
  21.    
  22.     void Update()
  23.     {
  24.         currentHealth = playerState.GetComponent<PlayerState>().currentHealth;
  25.         maxHealth = playerState.GetComponent<PlayerState>().maxHealth;
  26.  
  27.         float fillValue = currentHealth / maxHealth; // 100/100
  28.         slider.value = fillValue;
  29.  
  30.         healthCounter.text = currentHealth + "/" + maxHealth;
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement