Mouse released outside is not possible?

I am developing UI for my game. I started with simple Play button. When it is touched, the game will start, but if the user touches the play button and releases touch outside the play button nothing should be happen. How to implement such action. I tried with following code, but else statement in OnMouseUp never executes.

Is it such that whenever MouseDown is fired, MouseUp on that button will always be fired unless other collider of another game object is under touched?

Any guidance please?

if(Input.GetMouseButtonDown(0)) {
  
    hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  
    if(hit)    {
      
        if(transform.name == hit.transform.gameObject.name) {
          
            Debug.Log("Button touched");
        }
    }
}else
if(Input.GetMouseButtonUp(0)) {

    //hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

    if(hit)    {
      
        if(transform.name == hit.transform.gameObject.name) {
          
            Debug.Log ("touched up over same button.");
        }else {
            Debug.Log ("touched up not over same button.");
        }
    }
}

You shouldn’t use above code at all. Here is good tutorial by Unity make how to make buttons and such http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

Ok I agree this for button cause Its UI element. But what if we want same behaviour on GameObjects?

Got working with this,

if(Input.GetMouseButtonUp(0)) {
            
     MyGlobals.hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            
     if(MyGlobals.hit && transform.name == MyGlobals.hit.transform.gameObject.name) {
             Debug.Log ("touched up over same button.");
     } else {
           Debug.Log ("touched up not over same button.");
     }
}