- Home /
Does OnMouseUp() require OnMouseDown() to be triggered first?
This might be a bug, or I may just be looking at this wrong, but here's the behavior I'm seeing. I want to be able to click the mouse somewhere on the screen (not over my target) and drag the mouse over the target object. When I let go of the mouse button, I'd like things to happen. On my target object I have a script attached with this:
function OnMouseUp() {
print("Your Mouse button is up!");
}
Pretty simple. Yet, when I click somewhere on the screen, drag my mouse on top of the object, and then let the button go nothing happens. If I click the mouse down on top of my target and then let it go it works correctly. I assume this is because when my mouse is clicked while over the target it fires the OnMouseDown() function.
I've worked around the issue by setting a variable to true when OnMouseEnter() is triggered, and then in the update() I test is the variable is true && the GetMouseButtonUp(0).
function OnMouseEnter() { mouseIsOver = true; }
function Update() { if(mouseIsOver) { if(Input.GetMouseButtonUp(0)) { print("Your mouse button is up!"); } } }
Is this a bug, or am I missing something really obvious?
Answer by jacksmash2012 · Jul 29, 2010 at 06:00 PM
From the documentation:
"OnMouseUp is only called if the mouse was pressed down while over the same GUIElement or Collider."
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseUp.html
So perhaps it's because you're moving the mouse before releasing the mouse button that you're not getting the expected behavior?
I figured the answer was right in front of me! Guess my reading comprehension isn't what it used to be!
If this doesn't work for you, you need to code a custom raycast or detection inside of Update(), with GetButtonUp("mouse 0"). This will work no matter what, but you need to check if the cursor is over the object you want.
Your answer
Follow this Question
Related Questions
OnMouseDown() intended behaviour 1 Answer
NullReferenceException when looking at target in an Editor class (CustomEditor) 2 Answers
Asset store crash 2 Answers
Space Invaders style game bug. 1 Answer
BUG? Camera rotation on x set to 90! 0 Answers