Delegate and UI onClick.AddListener

Hello Unity community, I’ve come for some help concerning the implementation of a system I’ve been attempting to use.

I’ve got an item system that looks like this:

public class ItemLogic : MonoBehaviour {

	public class ItemData {

		public delegate void CB(); //Delegate 

		public int ID;
		public string Name;
		public string Description;
		public Type ItemType;
		public Sprite IconImg;
		public int MinAmount;
		public int MaxAmount;
		public CB UseCallback;
	}

	public enum Type {PrimWep, SecWep, MelWep, Ingredient, Crafting, Fluid, Food};
	int ItemID; //Auto increment IDs
	[HideInInspector] 	public List<ItemData> ItemDB;

	void BuildDatabase() {
		AddItemDB("Scrap Metal", "Random pieces of metal", Type.Crafting, "ScrapMetal", 1, 3, 0);
		ItemDB[(ItemID-1)].UseCallback = new ItemData.CB(DebugItem);

		//CALLING THIS WORKS SUCCESSFULLY AND OUTPUTS A DEBUG LINE
		ItemDB[(ItemID-1)].UseCallback(); 

}

Now the basics of this is that I use a delegate to use the item but right now I have it taking a test function for the sake of testing.

Now on the UI display side I run a for loop to iterate through all the objects and set a prefab.

for(int i=0; i<OurData.MaxInv; i++) {
	if(OurData.InvIDs *> -1) { //if the slot has an item*

_ /* SNIPPED UNNECESSARY CODE */_

_ Title.text = ItemScript.ItemDB[OurData.InvIDs*].Name;_
_ Desc.text = ItemScript.ItemDB[OurData.InvIDs].Description;
Quant.text = “x” + OurData.InvAmount.ToString();
Icon.sprite = ItemScript.ItemDB[OurData.InvIDs].IconImg;
//THIS IS A TEST THAT WORKS FINE*

ItemScript.ItemDB[OurData.InvIDs*].UseCallback()
//EVERYTHING WORKS FINE UNTIL THIS LINE*

UseBtn.onClick.AddListener(() => ItemScript.ItemDB[OurData.InvIDs*].UseCallback());
}
}*

Which to that line identified by the comment in the second script, I receive this error:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List1[System.Int32].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)*</em></em></em></em></em></em> <em><em><em><em><em><em>*Inventory+<FillChest>c__AnonStorey3.<>m__0 () (at Assets/Scripts/Inventory.cs:43)*</em></em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:137)*_</em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:602)*_</em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:744)*_</em></em></em></em></em> <em><em><em><em><em><em>*UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)*</em></em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)*_</em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)*_</em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)*_</em></em></em></em></em> <em><em><em><em><em>_*UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
Does anyone have any idea what could be causing this and also a fix for it?
Thank you.

Well, the first thing I would check would be whether OurData.MaxInv always equals OurData.InvIDs.Length. If not, then that would cause the problem in the for loop.

I don’t see where those variables are defined, but is there any reason why the loop doesn’t just go to OurData.InvIDs.Length to begin with?

It seems I have worked this out, for some reason this line does not work;

UseBtn.onClick.AddListener(() => ItemScript.ItemDB[OurData.InvIDs*].UseCallback());*

But this will fix it;
int TempID = OurData.InvIDs*;*
UseBtn.onClick.AddListener(() => ItemScript.ItemDB[TempID].UseCallback());
I still don’t quite understand why but this is the fix.