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?

void OnMouseOver () {
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)) {
Debug.Log (“Click!”);
}
}

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).