Search Unity

Question Hunger Bar not working :c

Discussion in 'Getting Started' started by real_Tepy, Mar 15, 2023.

  1. real_Tepy

    real_Tepy

    Joined:
    Aug 18, 2022
    Posts:
    2
    Hi, I have been working on Hunger Bar for My game from Unity learn pathway "junior Programmer".
    I cannot find why is my hunger bar not working, after 4 hours i copied whole code from step-by-step solution and it isn't still working. Unity doesn't give me any error, i have watched like 2 videos about creating some kind of bars and i still don't have a single idea where is problem. Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HungerBar : MonoBehaviour
    7. {
    8.     public Slider slider;
    9.     public int amountToBeFed = 2;
    10.     public int currentFedAmount = 0;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         slider.maxValue = amountToBeFed;
    16.         slider.value = 0;
    17.         slider.fillRect.gameObject.SetActive(false);
    18.     }
    19.     public void SetHunger(int Hunger)
    20.     {
    21.         currentFedAmount += Hunger;
    22.         slider.fillRect.gameObject.SetActive(true);
    23.         slider.value = currentFedAmount;
    24.         if (currentFedAmount >= amountToBeFed)
    25.         {
    26.             Destroy(gameObject, 0.1f);
    27.         }
    28.     }
    29. }
    30.  
    And here is another one:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DetectCollisions : MonoBehaviour
    6. {
    7.     // Update is called once per frame
    8.     void Start()
    9.     {
    10.    
    11.     }
    12.     // when two gameobjects collide with themselfs, they get destroyed
    13.     private void OnTriggerEnter(Collider other)
    14.     {
    15.         if (other.CompareTag("Player"))
    16.         {
    17.             GameManager.lives -= GameManager.livesDecrease;
    18.             GameManager.GameOvercondition();
    19.             Debug.Log($"Lives: { GameManager.lives }");
    20.             Destroy(gameObject);
    21.         }
    22.         if (other.CompareTag("Animal"))
    23.         {
    24.             other.GetComponent<HungerBar>().SetHunger(1);
    25.             Destroy(other.gameObject);
    26.             GameManager.score += 5;
    27.             Debug.Log(GameManager.score);
    28.         }
    29.     }
    30.  
    31. }
    32.  

    Thank you all for your time and help.
     
  2. Morris88

    Morris88

    Joined:
    Feb 25, 2014
    Posts:
    1
    Hello,

    Not sure if you're still having an issue but.

    I had a similar issue and had to play with the SliderCanvas Rect Transform in the UI on each animal it didn't seem to be the same as the settings the tutorial suggested. Each time I put the slidercanvas into the prefab it was tiny so had to mess wit the transform of each:
    upload_2023-4-15_15-17-29.png
    upload_2023-4-15_15-18-8.png
    upload_2023-4-15_15-19-27.png
    Might be worth playing with these to see if the hungar bars appear for you.