Input.GetMouseButtonUp(0) not detected after drag

Hi! I’m working on an implementation of a toolbar where you drag the item from your inventory onto the previous rect that the GUI has painted. But I’m having difficulty getting unity to recognize mouse release. It seems to work fine with GetMouseButton() but of course I’d prefer on release than on drag. I can’t seem to see why GetMouseButton() would differ from GetMouseButtonUp() in this instance. Any help is greatly appreciated!

var item = inventoryScript.GetDraggedItem();
if(Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && item != null){
	print("Reaching here is fine!");
	if(Input.GetMouseButtonUp(0)){
		print("It never reaches here :(");
		inventorySlot[number] = item;
	}
	
	/* This works, but is on drag instead of release
	if(Input.GetMouseButton(0)){
		inventorySlot[number] = item;
	}
	*/
}

The problem is here:

if(Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && item != null)

key point: Event.current.type == EventType.Repaint.

Releasing the mouse creates a ‘MouseUp’ event, which is obviously not the same as a Repaint event. This means that the mouse can never be in the process of getting released at the same time as you are in a Repaint Event!

Change this to

if(Event.current.type == EventType.MouseUp && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && item != null)

and use Event.button instead of Input.GetMouseButton, since you already know that the mouse was released.

In general, using Input.GetMouseButton inside of GUI code is a bad idea, because the GUI stuff gets called at a different rate and rhythm from the Update stuff that the Input class is designed to be used with.