Adding an item to a shop

jstoffer

New member
I am able to add items to normal inventories fine, but every time I try to add them to my shop inventory i get this null reference error.

My code...

Code:
if (itemAmount > 0)
        {
            var item = InventorySystemManager.GetItem(2405331441);
            var _state = item.GetAttribute<Attribute<int>>("State").GetValue();

            if (_state == 1)
            {
                m_Inventory.RemoveItem("Hoe", itemAmount);
     //           m_Morge.AddItem("Hoe", 1);                                 // This line of code goes to a normal storage and works fine
                m_MACS.AddItem("Hoe", 1);                                                        // This line of code references a shop inventory and give me the error
                var newItem = InventorySystemManager.CreateItem(keyItemDefinition);
                var itemInfo = new ItemInfo((1, newItem));
                var _sprite1 = itemInfo.Item.GetAttribute<Attribute<Sprite>>("Icon1").GetValue();
                var _sprite = itemInfo.Item.GetAttribute<Attribute<Sprite>>("Icon");
                var _state_ = itemInfo.Item.GetAttribute<Attribute<int>>("State");
                _sprite.SetOverrideValue(_sprite1);
                _state_.SetOverrideValue(0);
            }
        }

My Error...

Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateInventorySystem.Core.InventoryCollections.Inventory.get_MainItemCollection () (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/Inventory.cs:257)
Opsive.UltimateInventorySystem.Core.InventoryCollections.Inventory.AddItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo, Opsive.UltimateInventorySystem.Core.DataStructures.ItemStack stackTarget) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/Inventory.cs:460)
Opsive.UltimateInventorySystem.Core.InventoryCollections.Inventory.AddItem (System.String itemName, System.Int32 amount) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/Inventory.cs:507)
Clock.Reset () (at Assets/Scripts/Clock.cs:325)
Clock.Update () (at Assets/Scripts/Clock.cs:59)

Is there something specific I need to type when adding items to a shop inventory?
 
When pasting code please use the code snippet (I did it for you this time):
1705048391135.png

Looking at the error line, I think you simply haven't initialize the Shop Inventory.

Make sure to Intialize your Inventory before using it. This is done by default in Awake. But if not awaken, you can call it directly in code, using
Code:
m_Inventory.Initialize(false);
 
Top