Problem with OnMouseUp

I have, for example, 3 plane gameobjects in my scene. I want to have an operation like this:

mouse click on one of the objects and drag to another object, and release mouse click

So I implement OnMouseDown and OnMouseUp function in a script and place them in each of the objects.
However I got a problem. If I click on one of the objects and mouseover another object (say Object B), and then release the mouse click outside all objects, the OnMouseUp function still triggers and works on Object B. If I use OnMouseUpAsButton, it doesnt fire at all.

How to prevent OnMouseUp to trigger if I dont release the mouse click on objects? Any simple method other than checking the cursor position and calculate ranges. Thank you.

Short of Raycasting or some other cursor position check, I don’t think you can get what you want. Your description is not what I believe is happening. You get the OnMouseUp() in the same game object that got the OnMouseDown(). Even if the mouse is over another game object, the OnMouseUp() still goes to the original game object. You can tell if the ‘up’ is over the original game object using:

var over = false;
 
function OnMouseEnter() {
	over = true;
}

function OnMouseExit() {
	over = false;
}

function OnMouseUp()
{
	if (over) {
		Debug.Log("Button release over the same object as down");
	}
	else {
		Debug.Log("Button released not over the same object as down");
	}	
}

But this will not tell you if the Up is over another game object or none at all.

Helpful ThanK You

There are interfaces to do all this: