Search code examples
c#unity-game-engineonmouseup

OnMouseUp only works when object is clicked


I have some very simple code that, when an object is clicked (when the mouse is going from pushed to unpushed position), it adds force to an object. The information on OnMouseUp states that

"Note that OnMouseUp is called even if the mouse is not over the same GUIElement or Collider as the has mouse has been pressed down on."

That is exactly what I want. I want it to activate if I click anywhere on the screen. It is only working if I click on the object though. Am I misunderstanding something?

Here is the overall code (Very simple):

public Rigidbody2D Player;
private void OnMouseUp()
{
     Debug.Log("Test");
     layer.AddForce(transform.up * 1000);
}

Thanks,


Solution

  • Am I misunderstanding something?

    You got the answer to this under the comment section.

    I want it to activate if I click anywhere on the screen. It is only working if I click on the object though.

    OnMouseUp is not used for detecting click anywhere on the screen. You need to use one of the Input.GetMouseButtonXX functions in the Update function.

    Below is likely what you are looking for:

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Test");
            Player.AddForce(transform.up * 1000);
        }
    }