I don't know why but when I press play in Unity and play my game everything works fine. At the end when I defeat the boss, a panel shows up with a button that leads to the Main Menu scene, that works too. And in the Main Menu you have a "Play" button that leads to the level scene, obviously, and when you click it to play again I get all these errors:
I think the class does not find the Boss anymore(the game object is destroyed after being defeated) ? But I don't understand because I thought that with every "Play" the scene restarts itself and plus, the boss it's still in the hierarchy after clicking the "Play" button in the Main Menu:
This is the script:
public class HUDController : MonoBehaviour {
//Public variables
public Image[] lives;
public GameObject gameOverPanel;
public GameObject winPanel;
public Image oxBar;
public float oxSpeed;
public Text foodPoints;
public Slider bossHealthBar;
public GameObject fireworks;
[HideInInspector]
public bool stop;
[HideInInspector]
public float currentOx = 100f;
[HideInInspector]
public float maxOx = 100f;
//Private variables
private PlayerController player;
private BossController boss;
private float timer = 0;
void Start()
{
player = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerController> ();
boss = GameObject.FindGameObjectWithTag ("Boss").GetComponent<BossController> ();
gameOverPanel.SetActive (false);
winPanel.SetActive (false);
stop = false;
fireworks.gameObject.SetActive (false);
bossHealthBar.maxValue = boss.health;
bossHealthBar.gameObject.SetActive (false);
}
void Update () {
DisplayLives ();
DisplayFood ();
if (boss.startFire) {
bossHealthBar.gameObject.SetActive (true);
DisplayBossHealth ();
}
if (boss.health <= 0) {
timer += Time.deltaTime;
bossHealthBar.gameObject.SetActive (false);
if (timer > 1f) {
fireworks.gameObject.SetActive (true);
winPanel.SetActive (true);
}
}
float ratio = currentOx / maxOx;
oxBar.rectTransform.localScale = new Vector3 (ratio, 1f, 1f);
DecreaseOxygen ();
if (player.lives <= 0 || currentOx <= 0) {
stop = true;
currentOx = 0f;
gameOverPanel.SetActive (true);
}
}
//Shows lives available
void DisplayLives(){
for (int i = 0; i < lives.Length; i++)
{
if (i < player.lives) {
lives [i].enabled = true;
} else {
lives [i].enabled = false;
}
}
}
void DisplayFood(){
foodPoints.text = player.foodPoints.ToString();
}
void DecreaseOxygen(){
if (boss.bossDefeated) {
currentOx -= oxSpeed * Time.deltaTime;
}
}
void DisplayBossHealth(){
bossHealthBar.value = boss.health;
}
}
This is line 41:
bossHealthBar.maxValue = boss.health;
This is line 50:
if (boss.startFire) {
Maybe it's not because of the boss? if you need me to show you more code, let me know! please help.
EDIT: I added the whole script. This script is attached to my Hud (Canvas with my UI) and the script needs to access the script that is attached to the Boss (BossController) to access some variables for the UI (health, some booleans). It seems like the Scene does not load the object correctly because if I start the game from the Main Menu, I get the same errors. But if I just press play in Unity in the level scene, it works fine.
So I debugged the game and the BossController will not be assigned, it stays null:
And I don;t know why. The tag is correct and the boss is still in the hierarchy. And the player gets assigned perfectly, so I don't know what is different.