- Home /
Detecting mouse click on GameObject with any mouse button.
I'm a bit embarrassed for asking this, but it seems all the previous question are quite old and not really solve what I'm trying to do.
As simple as it gets: I got a cube with a script attached to it. I need to know when the player clicks the cube, so I can do stuff in a separate function. I'm running some tests, as the last time I touched the input system was some years ago, and each time I click the cube I got 6 output messages (Which I assume correlates to 6 calls to my separate function).
Code:
private bool[] mousePressed = { false, false, false };
// Update is called once per frame
void Update () {
mousePressed[0] = Input.GetMouseButtonDown(0);
mousePressed[1] = Input.GetMouseButtonDown(1);
mousePressed[2] = Input.GetMouseButtonDown(2);
doStuff();
}
private void doStuff() {
if (mousePressed[0] || mousePressed[1] || mousePressed[2]) {
clickTime = Time.time;
Debug.Log("Click!");
mousePressed[0] = mousePressed[1] = mousePressed[2] = false;
}
}
I know where the problem comes, as Update() is called once per frame, thus I got 4-6 calls to the function (and then to doStuff()) per frame. So I added the line to reset the mousePressed array:
mousePressed[0] = mousePressed[1] = mousePressed[2] = false;
But it's ignoring me, as I keep getting all the calls per frame.
And I'm clueless. I know I can use
OnMouseDown()
but it only detects left clicks (and I need both left and right clicks, and also this is somewhat personal now).
So, any help here?
Answer by Neurus_Ex · Nov 08, 2016 at 08:34 PM
Well, as pointed by @Eric5h5 on the comments, the problem wasn't on the code itself but in the scene, as I had several cubes with the same script.
Even the line
mousePressed[0] = mousePressed[1] = mousePressed[2] = false;
was useless, as, obviously, the array was updated in the Update() function.
(P.S.: @Eric5h5, if you update the answer with the several objects thing I can accept it as answer, as that was my problem/question).
Well...the thing is, the problem was in fact the code itself, not the scene, so this answer isn't really correct. By itself it's not an issue to have the same script running on multiple objects, it's the way you were doing it that was wrong. You said in your question, "I got a cube with a script attached to it. I need to know when the player clicks the cube [both left and right clicks], so I can do stuff in a separate function", and that's what my answer does. Feel free to have multiple cubes with that same script attached, and it works fine.
Yeah, and that's why I was asking the question I did. $$anonymous$$y question was not "I got this, I need this", but "I have a problem and I need to solve it." Turned out the problem wasn't about the script but about the scene, thus the question got answered.
So, whatever, dude. Your answer did not answered my question (even less the piece of code without comments), the comments did.
Answer by Eric5h5 · Nov 07, 2016 at 12:52 AM
void OnMouseOver () {
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)) {
Debug.Log ("Click!");
}
}
Care to explain a bit why does this work?
I can't access right now to my project, but for what the documentation says, this is called each frame the mouse is over the object. Won't I'll get the same result (a click will generate several "Click!" outputs) with an additional set of calls to the function as soon as the mouse gets "above" the object?
I don't really follow, sorry. It works because it checks for left/right/middle clicks if the mouse is over the object. The result is essentially the same as On$$anonymous$$ouseDown, except for any button. Nothing else happens.
Did you read my whole question? $$anonymous$$y problem comprises that using get$$anonymous$$ouseButtonDown I got 4-6 calls per click (as "I'm clicking" the cube for 4-6 frames).
Your answer
Follow this Question
Related Questions
detecting if the mouse is inside any gui element 1 Answer
Raycast hit not detected on cube 1 Answer
Right mouse detection 2 Answers
Mouse Over & Mouse Click 1 Answer
Unity mouse input gradually becomes off 0 Answers