Button OnClick()

Im trying to get the size of an array when i use the ‘On Click ()’ component of a button. The void runs (prints my Debug.Log) but it returns my array as 0. When i use an Input.GetKeyDown to read the count size in the same instance it returns the correct value. But when i try to use the SAME EXACT FUNCTION the KeyDown was running threw the ‘On Click ()’ component it returns 0;

As you can see from the images, On Click is calling UseItem inside of UIManager.

I ran the useItem void threw an input and it works fine. Returns 0 when using onClick


Another way of asking whats wrong is as followed

When i try to remove a GameObject from a list with the UI elements On Click component, the function that fires won’t remove the object from the list. It will only remove the displayed object with Destroy(). Then will return 0 on the List.Count.

BUT if i fire a function with an input doing the same code to remove the object using ‘EventSystem.current.currentSelectedGameObject’ it will work 100% perfect. Why wont the same exact code work on the fired function of On Click?

if (Input.GetKeyDown(KeyCode.Z))
{
var obj = EventSystem.current.currentSelectedGameObject;
Destroy(obj);
itemsInInv.Remove(obj.gameObject);
Debug.Log(itemsInInv.Count);
Debug.Log("You used a " + obj.name);
sortItems();
}

^ thats the code. Works fine if i push ‘Z’ after clicking an object. But if i run that code directly from the fired function it will return 0 on the list.count.

I couldnt find a solution to my problem and have no idea what the problem was, but i manually added an event listener listening for the specific function instead of using the built in one on the inspector.