Button little bug

hi, i have a little bug with my buttons, when i click on one of them (2 actions GetMouseButtonDown and GetMouseButtonUp) and release it works perfectly, but when i click and drag and release out of the button it bugs. it isn’t a big bug but as my button get smaller when down and return to his size when Up, it doesn’t work with the bug.

I want when i release out of the button, it returns to his size. Any idea to solve this problem.

Thanks

The problem is that you are checking that you hit the button on mouse up so obviously if you are not releasing the mouse over the button line 15 will not return true. You could simply remove the line 15 if statement or if you want to be a little more rigorous and avoid problems down the line then use a boolean value to check whether your object is shrunk:

var isShrunk : boolean = false;

function Update ()
{
    var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
 
    if (Input.GetMouseButtonDown(0))
    {
      if(hit.collider.name == "Button")
       {
        gameObject.Find("Button").transform.localScale = Vector3 (0.9, 0.9, 1);
		isShrunk = true;
       }
    }
 
    if (Input.GetMouseButtonUp(0))
    {
       if(isShrunk)
       {
         gameObject.Find("Button").transform.localScale = Vector3 (1, 1, 1);
		 isShrunk = false;
        Application.LoadLevel ("Level 1");
       }
    }
}

Scribe

try this (I haven’t actually run this code)

The problem (to my understanding) is that sometimes when you release the left button outside of the box’s collider the last code wouldn’t be run…

private bool isButtonPressed = false;

function Update ()
{
    var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
 
    if (Input.GetMouseButtonDown(0))
    {
      if(hit.collider.name == "Button")
       {
        isButtonPressed = true;
        gameObject.Find("Button").transform.localScale = Vector3 (0.9, 0.9, 1);
       }
    }
 
    if (Input.GetMouseButtonUp(0))
    {
       if(isButtonPressed)
       {
         isButtonPressed = false;
         gameObject.Find("Button").transform.localScale = Vector3 (1, 1, 1);
        Application.LoadLevel ("Level 1");
       }
    }
}

Hope this helps :slight_smile: