In Match3 toy move the cubes. If during the movement of the cubes to click on one more - the movement stops. One more time - and the third cube begins to move as the second. While looking for the cause of this bug, the question arose - is it not easier to disable mouse clicks while the cubes are flying from place to place? And how to implement this on the touchscreen?

public void OnMouseDown() { //Если этот кубик не был выбран - он выбирается if (!isSelected && (myController.selectedPieces[0] == null || myController.selectedPieces[1]== null))// Вроде бы должен не выбирать кубик, если массив заполнен, но он всё равно выбирается. { isSelected = true; } else { //Если кубик был выбран ранее и щёлкнули на него же - он перестает быть выбранным isSelected = false; isMoving = false; / } myController.selected(this.gameObject, isSelected); //Вызываем скрипт gameController, передавая ему информацию о кубике. Вызван или не вызван. } 
  • Well, put a boolean flag, what's the catch? - VladD
  • @VladD in a little more detail, please. I know what a boolean variable is, but I don’t know how to disable void with it. - Dmitrii
  • one
    public void OnMouseDown() { if (isMoving) return; ...... остальная логика } public void OnMouseDown() { if (isMoving) return; ...... остальная логика } - Alexey Shimansky
  • one
    Well, as if this is a well-known technique, so once you ask, there is a chance that you used it, but there are subtleties because of which it did not work. - VladD
  • one
    Do you turn off the active object if you clicked on another cube running in myController ? In theory, your handler works only for the object that was clicked on, if other cubes do not behave adequately, then the problem is in the controller that controls the current cube. - KingPeas

1 answer 1

Apparently, you need this pattern:

 bool isMoving = false; // static по идее не нужен public void OnMouseDown() { if (isMoving) return; try { isMoving = true; // тут остальной код } finally { isMoving = false; } } 

try / finally needed in order to reset isMoving even if the method isMoving with the exception.