Search code examples
unity-game-enginegame-physicsgame-development

Wait for some time in between onMouseDown() function in unity2D


I have built this 2D project in unity where you tap on blocks and they destroy using the onMouseDown() function. My problem is after tapping a block and it destroys, how can I make the player wait for a certain amount of time before he can tap on another block in the game. I have tried using PlayerPrefs and subtracting Time.deltaTime from a certain float variable but it did not work.

Note: all the blocks share the same destroy script!!!


Solution

  • float waitTime = 1.5f;
    static float lastClickTime = float.NegativeInfinity;
    
    void OnMouseDown ()
    {
        float time = Time.time;
        if( time > ( lastClickTime + waitTime ) )
        {
            lastClickTime = time;
            DestroyThisBlock();
        }
    }