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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HungerBar : MonoBehaviour
{
public Slider slider;
public int amountToBeFed = 2;
public int currentFedAmount = 0;
void Start()
{
slider.maxValue = amountToBeFed;
slider.value = 0;
slider.fillRect.gameObject.SetActive(false);
}
public void SetHunger(int Hunger)
{
currentFedAmount += Hunger;
slider.fillRect.gameObject.SetActive(true);
slider.value = currentFedAmount;
if (currentFedAmount >= amountToBeFed)
{
Destroy(gameObject, 0.1f);
}
}
}
And here is another one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectCollisions : MonoBehaviour
{
// Update is called once per frame
void Start()
{
}
// when two gameobjects collide with themselfs, they get destroyed
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
GameManager.lives -= GameManager.livesDecrease;
GameManager.GameOvercondition();
Debug.Log($"Lives: { GameManager.lives }");
Destroy(gameObject);
}
if (other.CompareTag("Animal"))
{
other.GetComponent<HungerBar>().SetHunger(1);
Destroy(other.gameObject);
GameManager.score += 5;
Debug.Log(GameManager.score);
}
}
}
Thank you all for your time and help.