0

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:

enter image description here

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:

enter image description here

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:

enter image description here

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
bonishadelnorte
  • 75
  • 1
  • 1
  • 7
  • 1
    @HimBromBeere it's not only a NullPReferenceExsception, it only happens after restarting the game through a button!!! I haven;t seen that question yet -.- – bonishadelnorte Oct 30 '18 at 10:19
  • @bonishadelnorte Agreed. Which line is 41? and could you include code from your update() aswell? – Skdy Oct 30 '18 at 10:22
  • I sympathize with your problem but that is unfortunately besides the point, there are an infinite *reasons* why you're getting a `NullReferenceException` but the **most useful** answer that we can provide is always going to be *debug, debug, debug*. Start by finding out *what* in that method is `null`, then go from there. – Lasse V. Karlsen Oct 30 '18 at 10:22
  • I think your HUDController.cs is not reloaded when reloading the scene. It looks like that script is applied on a game object that is never destroyed. So, your boss is assigned when you start the game for first time, but when you reload the game then that **assigned boss** is destroyed. So, it's not found anymore. – Bhavin Panara Oct 30 '18 at 11:06
  • yes show me speciffically lines 41 & 50 , if your boss is still in the hierarchy, are you setting its tag programmatically? try setting the bosses tag in its own script in Awake() – Technivorous Oct 30 '18 at 12:32
  • @BhavinPanara so how do I fix this? Also, when I load the scene by pressing the "Play" button in the Main Menu, the boss is still in the Hierarchy and it is enabled, which I dont understand because it should be found then. And it is correctly tagged and everything. – bonishadelnorte Oct 31 '18 at 07:12
  • @PrinceOfRavens I added more information to the question. I set the tag in the Inspector, not through code. I only use the tag in the code to find the object (FindObjectWithtag). How do I set the tag in awake? – bonishadelnorte Oct 31 '18 at 07:14
  • @ to the rest, I'm going to debug the game now in MonoDevelop to see what's happening. – bonishadelnorte Oct 31 '18 at 07:15
  • okay nevermind I just solved it ^^' I had two objects with the tag "Boss" so Unity was confused on which one to pick, so it stayed null. I changed the tags and now it's working fine – bonishadelnorte Oct 31 '18 at 07:37

0 Answers0