NullReferenceException for RPG.Core.Health.Die()

If an anemy is dead I get a NullRefernceException when the game starts or I switch to a level with dead characters in it.

I’ve tried saving and loading manually with the “s” and “l” buttons and it works great. But if I load the game or a scene I get “Loading from…” followed by the following error.

RPG.Core.Health.Die () (at Assets/Scripts/Core/Health.cs:44)
RPG.Core.Health.RestoreState (System.Object state) (at Assets/Scripts/Core/Health.cs:58)
RPG.Saving.SaveableEntity.RestoreState (System.Object state) (at Assets/Scripts/Saving/SaveableEntity.cs:37)
RPG.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/Scripts/Saving/SavingSystem.cs:68)
RPG.Saving.SavingSystem.Load (System.String saveFile) (at Assets/Scripts/Saving/SavingSystem.cs:24)
RPG.Saving.SavingWrapper.Load () (at Assets/Scripts/Saving/SavingWrapper.cs:30)
RPG.SceneManagement.Portal+d__9.MoveNext () (at Assets/Scripts/SceneManagement/Portal.cs:53)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

And I was just about to paste my code into this section when I realized that I had the following in my Health-class

public void Start()
        {
            animator = GetComponent<Animator>();
        }

So changed to Awake instead to make sure the animator is connected before loading happens.

public void Awake()
        {
            animator = GetComponent<Animator>();
        }

I thought I’d post this anyway If anyone else has a similar problem

2 Likes

Yes, once we get into the SavingSystem, it’s important to cache references in Awake(0 whenever possible.

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

Privacy & Terms