Advertisement
LeeMace

Health/Hunger Bar

Dec 13th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class AnimalHunger : MonoBehaviour
  7. {
  8.     public Slider hungerSlider;
  9.     public int amountToBeFed;
  10.     private int currentAmountFed = 0;
  11.     private GameManager gameManager;
  12.  
  13.  
  14.     void Start()
  15.     {
  16.         hungerSlider.maxValue = amountToBeFed;
  17.         hungerSlider.value = 0;
  18.         hungerSlider.fillRect.gameObject.SetActive(false);
  19.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
  20.     }
  21.  
  22.     public void FeedAnimal(int amount)
  23.     {
  24.         currentAmountFed += amount;
  25.             hungerSlider.fillRect.gameObject.SetActive(true);
  26.         hungerSlider.value = currentAmountFed;
  27.  
  28.         if(currentAmountFed >= amountToBeFed)
  29.         {
  30.             gameManager.AddScore(amountToBeFed);
  31.             Destroy(gameObject, 0.1f);
  32.         }
  33.     }
  34. }
Tags: health bar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement