NullReferenceException on load from Weapon Load

Hey All,
I’ve been working through the RPG Core classes and got pretty much to the end, however, the save system is causing an issue now with loading weapons. I’ve set up several Debu.Logs in RestoreState:

        public void RestoreState(object state)
        {
                Debug.Log("Loading state: " + state);
            string weaponName = (string)state;
                Debug.Log("Loading weaponName: " + weaponName);
            Weapon weapon = UnityEngine.Resources.Load<Weapon>(weaponName);
                Debug.Log("Loading Weapon: " + weapon);
            EquipWeapon(weapon);

And the first two Debugs return Unarmed since that is the equipped weapon, however, the 3rd Debug returns nothing and the error message is:

NullReferenceException: Object reference not set to an instance of an object
RPG.Combat.PlayerFighter.AttachWeapon (RPG.Combat.Weapon weapon) (at Assets/Scripts/Combat/PlayerFighter.cs:41)
RPG.Combat.PlayerFighter.EquipWeapon (RPG.Combat.Weapon weapon) (at Assets/Scripts/Combat/PlayerFighter.cs:36)
RPG.Combat.PlayerFighter.RestoreState (System.Object state) (at Assets/Scripts/Combat/PlayerFighter.cs:94)
RPG.Saving.SaveableEntity.RestoreState (System.Object state) (at Assets/Scripts/Saving/SaveableEntity.cs:39)
RPG.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary2[TKey,TValue] state) (at Assets/Scripts/Saving/SavingSystem.cs:85) RPG.Saving.SavingSystem.Load (System.String saveFile) (at Assets/Scripts/Saving/SavingSystem.cs:35) RPG.SceneManagement.SavingWrapper.Load () (at Assets/Scripts/Core/SavingWrapper.cs:27) UnityEngine.Events.InvokableCall.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:178) UnityEngine.Events.UnityEvent.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

I’m not sure what the issue is. Does anyone have any thoughts on what can be done to fix it or diagnose the issue?

Thanks

Never mind, I worked out what was wrong. I forgot to create the Resource fonder.

Sorry for the delay on this. Good job on finding the answer! Anything using the Resources.Load system have to be in a folder named Resources. In the Inventory course, we’ll improve on this design by using the same sort of ID system as we use for SaveableEntitites and loading by IDs. They’ll still need to be in a Resource folder, but the system is much more efficient.

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

Privacy & Terms