Health destroyed when stopping game - RPG

Hi All,

I’ve got an unusual error coming up that’s got me a bit stumped.

I can run the game fine, everything looks to function correctly but then on stopping the game I get errors.

First I get “Can’t remove Health (Script) because CombatTarget (Script) depends on it” followed by null reference exceptions as below:

NullReferenceException: Object reference not set to an instance of an object
RPG.Attributes.Health.GetFraction () (at Assets/Scripts/Attributes/Health.cs:91)
RPG.Attributes.HealthBar.Update () (at Assets/Scripts/Attributes/HealthBar.cs:15)

and also a couple of missing reference exceptions as below:

MissingReferenceException: The object of type ‘BaseStats’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Component.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Component.bindings.cs:42)
RPG.Stats.BaseStats.CalculateLevel () (at Assets/Scripts/Stats/BaseStats.cs:110)
RPG.Stats.LevelDisplay.Update () (at Assets/Scripts/Stats/LevelDisplay.cs:20)

MissingReferenceException: The object of type ‘Health’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Component.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Component.bindings.cs:42)
RPG.Attributes.Health.GetMaxHealthPoints () (at Assets/Scripts/Attributes/Health.cs:81)
RPG.Attributes.HealthDisplay.Update () (at Assets/Scripts/Attributes/HealthDisplay.cs:20)

Now I know this is because Health is being destroyed but I can’t figure out why Health is being destroyed when I stop the game.

There are 5 null reference exceptions and 5 enemies in my scene. I can add null checks in the methods but I still want to know why it’s being destroyed on finish haha.

TIA

Actually all of the components are being destroyed when you stop the game, as they only exist in the context of the game…

All of these MissingReference errors are occuring in your UI scripts, and I’m willing to bet they are in the Update method. Your update likely looks something like this:

void Update()
{
     display.text = health.GetCurrentPoints + "/" health.GetMaxHealthPoints();
}

(the names of the components and exact syntax will vary, this is just the general idea of how these things work.)

Now here’s the problem, when the game ends, each of the game objects is destroyed (I know it looks like it’s right there in the scene heirarchy, but that’s the version from the scene file itself). They’re not all destroyed in the same instant, however, they are destroyed one at a time. In your particular case, you’re unlucky enough that the characters components are being destroyed before the UI components, but Update() is still running.

Try adding null reference checks to the start of each of the Update() methods in your UI scripts.
So if the script reads.

     if(health==null) return;

or

    if(basestats==null) return;

Ah okay, I assumed it was a direct Destroy or something rather than a bi product of stopping the game. This is really useful info thank you! I’ll add null checks as you say.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.