Search Unity

Swords and Shovels Inventory system Null Reference Exception, Advice needed

Discussion in 'Community Learning & Teaching' started by Deleted User, Sep 15, 2020.

  1. Deleted User

    Deleted User

    Guest

    Swords and Shovel - Creating a Character Inventory System in Unity

    After following these tutorials and making sure the scripts were corrects, it appears that the inventory is broken (or is it?).

    What should happen: on clicking on a button in the hotbar, the sword added to that button should be equipped by the player.

    What happens: when I click on the button in the hotbar nothing happens and I get the following null reference exception:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. ItemPickUp.UseItem () (at Assets/Scripts/4. Item Loot/MonoBehaviours/ItemPickUp.cs:62)
    3. CharacterInventory.TriggerItemUse (System.Int32 itemToUseID) (at Assets/Scripts/5. Inventory/Monobehaviours/CharacterInventory.cs:277)
    that I am investigating, immediately followed by these lines:
    Code (CSharp):
    1. UnityEngine.Events.InvokableCall`1[T1].Invoke (T1 args0) (at /home/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:221)
    2. UnityEngine.Events.CachedInvokableCall`1[T].Invoke (System.Object[] args) (at /home/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:359)
    3. UnityEngine.Events.UnityEvent.Invoke () (at /home/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:70)
    4. UnityEngine.UI.Button.Press () (at /home/anne/Applications/Unity Technologies/2020.2.0a15/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
    5. UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /home/anne/Applications/Unity Technologies/2020.2.0a15/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
    6. UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /home/anne/Applications/Unity Technologies/2020.2.0a15/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
    7. UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at /home/anne/Applications/Unity Technologies/2020.2.0a15/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:262)
    8. UnityEngine.EventSystems.EventSystem:Update() (at /home/anne/Applications/Unity Technologies/2020.2.0a15/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:384)
    So, what is it all about? Is something broken in Unity?

    Note: the first lines seem to point to a real error since when I load the project in an older version of Unity (2017.4.1) and I click on the button in the hotbar, nothing happens but I get no error message at all, no indication about why it's not working and no null reference exception.

    Advice from experts is highly welcome! :)
     
    Last edited by a moderator: Sep 15, 2020
  2. Deleted User

    Deleted User

    Guest

    I found and fixed the errors in the first lines but I also discovered that the script works perfectly if it's added directly to the Hero.

    In the project, it's supposed to be applied to a prefab that is added as a child to the Hero but if we do that, trying to equip the weapon either by using the shortcut (Alpha1) or clicking on the button, produces an "out of bound of the array" error that I didn't manage to fix.

    So, if you are doing the Sword and Shovel courses and you have trouble with the inventory, just add the CharacterInventory script directly to the player, make sure there is no error in the code and everything should be fine.