Getting NullReferenceException in Fighter.cs

Getting a NullReferenceException after implementing the LazyValue on the Fighter script.

The line is:

currentWeapon.value = weapon;  

this is within the EquipWeapon method. Here is the stack trace:

NullReferenceException: Object reference not set to an instance of an object
RPG.Combat.Fighter.EquipWeapon (RPG.Combat.Weapon weapon) (at Assets/Scripts/Combat/Fighter.cs:63)
RPG.Combat.Fighter.RestoreState (System.Object state) (at Assets/Scripts/Combat/Fighter.cs:152)
RPG.Saving.SaveableEntity.RestoreState (System.Object state) (at Assets/Scripts/Saving/SaveableEntity.cs:33)
RPG.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/Scripts/Saving/SavingSystem.cs:78)
RPG.Saving.SavingSystem+<LoadLastScene>d__0.MoveNext () (at Assets/Scripts/Saving/SavingSystem.cs:22)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RPG.SceneManagement.SavingWrapper:Awake() (at Assets/Scripts/Scene Management/SavingWrapper.cs:15)
UnityEngine.Object:Instantiate(GameObject)
RPG.Core.PersistentObjectSpawner:SpawnPersistentObject() (at Assets/Scripts/Core/PersistentObjectSpawner.cs:22)
RPG.Core.PersistentObjectSpawner:Awake() (at Assets/Scripts/Core/PersistentObjectSpawner.cs:16)  

Steps to reproduce:

  1. start the game
  2. save the game
  3. stop the game
  4. start the game (this will load from last save)

Not sure if I have to do some additional digging, or if this is doing this for everybody. If you would like more info, let me know. My repo is currently private, or I would link that as well.

2 Likes

After more looking, I can see that it’s coming from the RestoreState, and when I try to

print (currentWeapon.value);

in restore state, it prints the value, but I get a new NullReferenceException at the print line that I just created (the one in my code block in this comment). Still looking to see if I can solve this.

1 Like

Also to note, the code appears to be working just fine, but still don’t understand why I’m getting the NullReferenceException.

2 Likes

@lometur i’m getting a NullReferenceExeption when ever i move in the editor and not at runtime but the errors are not stopping the code from running so i will try to hunt the bug down.

Lloyd Risper

@Lloyd_Risper I’ll have to go back and remember the race condition issue with why we changed the Start to Awake in PersistentObjectSpawner, but basically, calling the SpawnPersistentObject in Awake was the problem. I switched it to Start instead, and I’m not getting the error, and it’s restoring correctly. I’ll look to see if there is a more robust solution, or if this is okay.

2 Likes

@Lloyd_Risper after more consideration, I think changing to OnEnable is a better option, as it still instantiates our objects prior to Start, but after Awake. I switched mine to OnEnable, and it appears to be working as expected with no errors.

PersistentObjectSpawner.cs

        private void OnEnable() {
            if (hasSpawned) return;
            SpawnPersistentObject();
            hasSpawned = true;
        }

2 Likes

Nevermind, it still causes an issue (sometimes) in OnEnable… so I changed it to Start

3 Likes

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

Privacy & Terms