Calling ItemSlotCollectionView.AddItem before gameObject enabled

Zaddo

Active member
There is a limitation of the ItemSlotCollectionView that stops AddItem working if called before the panel has been displayed. I am just posting this in case someone else hits this problem.

The ItemViewSlot component initializes the variable m_ItemViewSlotRestrictions in the Awake() method. If ItemSLotCollectionView.AddItem is called before the Awake gets called, then when it calls ItemViewSlot.CanContain, it will throw an error.

This can be fixed by calling Awake() at the beginning of the method ItemViewSlot.CanContain().

Code:
        public bool CanContain(ItemInfo itemInfo)
        {
            // Initialise componenent if Awake han't been called yet.
            // This can happen when inventory is added from code and the Panel has not yet been displayed
            if (m_ItemViewSlotRestrictions == null) Awake();

            for (int i = 0; i < m_ItemViewSlotRestrictions.Length; i++) {
                if (m_ItemViewSlotRestrictions[i].CanContain(itemInfo) == false) { return false; }
            }

            return true;
        }
 
Last edited:
Thank you for pointing it out and showing a work around.
I'll add it to the list of things to investigate.
 
Top