Null Reference Exception Error

So I finished the course, and when I start my game in the editor, it throws a Null Reference Exception for the AttachWeapon function.

The game loads with just a “default weapon” but I’m unsure where the actual issue is. Here’s my error

NullReferenceException: Object reference not set to an instance of an object
Sanctuary.Harry.Combat.Fight.AttachWeapon (Sanctuary.Harry.Combat.WeaponConfig weapon) (at Assets/Harry/Scripts/Combat/Fight.cs:157)
Sanctuary.Harry.Combat.Fight.EquipWeapon (Sanctuary.Harry.Combat.WeaponConfig weapon) (at Assets/Harry/Scripts/Combat/Fight.cs:144)
Sanctuary.Harry.Combat.Fight.RestoreState (System.Object state) (at Assets/Harry/Scripts/Combat/Fight.cs:169)
GameDevTV.Saving.SaveableEntity.RestoreState (System.Object state) (at Assets/Harry/Scripts/Saving/SaveableEntity.cs:64)
GameDevTV.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/Harry/Scripts/Saving/SavingSystem.cs:106)
GameDevTV.Saving.SavingSystem+<LoadLastScene>d__0.MoveNext () (at Assets/Harry/Scripts/Saving/SavingSystem.cs:34)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <ab7de6937a4448b0a6ccd59e9820599a>:0)

Now everything works, but it isn’t populating the default weapon into my inventory slot (I’m 90% sure it’s because there’s nothing in the equipment slot for the weapon)

This looks like it’s happening during a restore…

The most common cause for this is an Inventory Item not being in a Resources folder, meaning that it can’t be retrieved through the Resources system at runtime.

Make sure that your weapons (all your inventory items) are in folders named Resources (or are in a folder under a Resources folder…

So I think I compared my fighter script to the finished fighter script in the git repo, and my fighter script has a restore, where the fighter script in the repo does not.

All my weapons are in a resources folder, without a doubt. I’m just wondering if there was a point where we removed a restore function in fighter

Once we have the Inventory System, the ISaveable in Fighter is actually redundant. It makes more sense to rely on the Equipment to equip the weapon. We actually point this out, but not until the Shops and Dialogues course.

So I tested something.

I deleted the save data that was in my local low folder, and the error disappeared. IDK if it was just something with an old version that made it go wonky?

It’s quite possible. If there is an error in the CaptureState process, then the SaveFile is ruined and needs to be replaced

Privacy & Terms