0

Trying to finish of a game development project but having a couple issues that despite my last couple days of research I cannot figure out what the issue is.

Firstly being the error that occurs every time I die as it is meant to redirect you to a game over screen with your score which I save via PlayerPref

// THIS IS IN THE CharacterHealth.cs SCRIPT
void Die()
{
    CurrentHealth = 0;
    _characterXP.OnDeath();
    SceneManager.LoadScene("NewGameOver");
}

// THIS IS IN THE CharacterXP.cs SCRIPT
public void OnDeath()
{
    PlayerPrefs.SetInt("EndLevel", currentLevel);
}

But I keep getting the error: NullReferenceException: Object reference not set to an instance of an object before I can even get redirected to the Game Over screen.


Second issue being that I have two enemies that I placed onto the scene and the rest come from a script which spawns them every 5 to 10 seconds. The ones that are spawned from the script have the (Clone) in their name and I think that may be disrupting the script that works with the two pre-spawned monsters where killing them will give the player experience.

# IN THE EnemyHealth.cs SCRIPT
void Dead()
{
    _characterXP.GainExp(40);
    Destroy(gameObject);
}

# IN THE BossHealth.cs SCRIPT
void Dead()
{
    _characterXP.GainExp(120);
    Destroy(gameObject);
}

# IN THE CharacterXP.cs SCRIPT
public void GainExp(float expThatWasGained)
{
    CurrentExp += expThatWasGained;
}

I have used the existing resources regarding these subjects but I still cannot find a solution, the reason why I asked a separate question.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Obahar
  • 107
  • 1
  • 1
  • 10
  • may I ask what specific code block is throwing the error? – Frontear May 24 '18 at 15:31
  • The void Die() part, specifically _characterXP.OnDeath(); – Obahar May 24 '18 at 15:41
  • Please recheck in the Unity GUI that `_characterXP` is populated. You may have left it blank. In the `Script` tab, where the public field box is displayed, make sure it links with your "character". – Frontear May 24 '18 at 15:44
  • I reset the component and placed everything back in again, that bit works now- I guess something went wrong with that one; except the value still isn't being transferred to the Game Over screen's text. – Obahar May 24 '18 at 15:54
  • I'm not sure what you mean. Do you mean that the `CurrentHealth`, which is displayed on-screen, doesn't update after switching screens? If so, are you making sure that the field `CurrentHealth` is actually a part of player? From the way it is written, it seems that it is being controlled by something different. Is it supposed to be `_character.CurrentHealth`? Additionally, does switching scenes freeze the text? – Frontear May 24 '18 at 15:57
  • When the player dies the currentLevel is saved to PlayerPrefs of which is then meant to be loaded again on the GameOver screen to show the player what their current level was when they died (Using currentLevel as the measurable stat) I'm not sure what you mean by switching scene freezing the text, the issue I'm having is the new text on the GameOver screen just isn't reflective of the player's currentLevel – Obahar May 24 '18 at 16:05
  • 1
    Can you check if `currentLevel` is populated? – Frontear May 24 '18 at 16:20
  • Well, that fixed that issue. It was because I had a typo in my PlayerPrefs call on the GameOver screen. Any idea about the second issue? Where killing the cloned version of my prefabs doesn't give exp when killed and don't do damage to the player on contact despite the pre-placed ones being able to. – Obahar May 24 '18 at 16:25

0 Answers0