I’m looking for help with a bug that I’m having in Unity currently with an inventory system that I have adapted from Brackey’s RPG videos. Instead of having open slots, I’m instantiating and destroying UI elements for an inventory system similar to PUBG’s system where it has an unlimited length of items but the player is limited by the capacity of his backpack/worn items.
When equipping an equipment item, when one is already in it’s slot (eg. replace a backpack with another one), the equipped item is added to the inventory but is not replaced and an error is thrown. The error is in InventorySlot.AddItem and is a MissingReferenceException. Normally, this would be easily fixed by not trying to change something that doesn’t exist, but I wrapped the inside of the function in an if statment with a null check. That’s why I’m confused. I put the code in a GitHunb repository and screenshots here so it hopefully makes sense to someone.
tl;dr big bug pls help
In the screenshot i”m clicking on the large backpack, and the medium backpack is in the inventory. On click the medium backpack is added to the inventory (as it should), but the large isnt removed and the medium in the slot isnt replaced. On top of this, an empty item UI element prefab is added with no item attached (the qqq items).
Text from the bug:
MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.EventSystems.UIBehaviour.IsActive () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/UIBehaviour.cs:22) UnityEngine.UI.Graphic.SetVerticesDirty () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:100) UnityEngine.UI.Text.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:138) InventorySlot.AddItem (.Item newItem) (at Assets/InventorySlot.cs:37) InventoryUI.UpdateUI () (at Assets/InventoryUI.cs:45) Inventory.Add (.Item item) (at Assets/Scripts/Inventory.cs:56) EquipmentManager.Unequip (Int32 slotIndex) (at Assets/EquipmentManager.cs:53) EquipmentManager.Equip (.Equipment newItem) (at Assets/EquipmentManager.cs:36) Equipment.Use () (at Assets/Scripts/Equipment.cs:16) InventorySlot.UseItem () (at Assets/InventorySlot.cs:64) UnityEngine.Events.InvokableCall.Invoke (System.Object args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()