Null reference error

Hello everyone!

I’ve been getting this error after following video #124

RPG.Combat.Weapon.Spawn (UnityEngine.Transform rightHand, UnityEngine.Transform leftHand, UnityEngine.Animator animator) (at Assets/Scripts/Combat/Weapon.cs:44)
RPG.Combat.Fighter.EquipWeapon (RPG.Combat.Weapon weapon) (at Assets/Scripts/Combat/Fighter.cs:50)
RPG.Combat.Fighter.Start () (at Assets/Scripts/Combat/Fighter.cs:24)
NullReferenceException: Object reference not set to an instance of an object
RPG.Combat.Weapon.Spawn (UnityEngine.Transform rightHand, UnityEngine.Transform leftHand, UnityEngine.Animator animator) (at Assets/Scripts/Combat/Weapon.cs:44)
RPG.Combat.Fighter.EquipWeapon (RPG.Combat.Weapon weapon) (at Assets/Scripts/Combat/Fighter.cs:50)
RPG.Combat.Fighter.RestoreState (System.Object state) (at Assets/Scripts/Combat/Fighter.cs:130)
RPG.Saving.SaveableEntity.RestoreState (System.Object state) (at Assets/Scripts/Saving/SaveableEntity.cs:39)
RPG.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/Scripts/Saving/SavingSystem.cs:85)
RPG.Saving.SavingSystem+<LoadLastScene>d__0.MoveNext () (at Assets/Scripts/Saving/SavingSystem.cs:23)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)




thank you in advance!! :slight_smile:

i tried changing the start method in fighter.cs to awake but it did not help

The error message is indicating that rightHand isn’t set, which is odd since you’re showing images of an inspector where they are set… Is it possible you have more than one enemy?

One of the tricky parts about Error messages is that they tell you the script, but not the object… Let’s see if we can narrow this down.
Let’s add a Debug statement to the first line in DestroyOldWeapon:

if(!righthand)
{
    Debug.Log($"{name} has no right hand transform.");
    return;
}

This will show which item is missing a transform.

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

Privacy & Terms