Adding A 1 Second Click Delay

This code is placed on boxes that are destroyed when clicked. I want to add a one second delay before it’s possible to click again. How would you handle this?

using UnityEngine;
using System.Collections;

public class destroy : MonoBehaviour {

    void OnMouseDown () {
        Destroy (gameObject);
    }
}

I’d simply keep track of when the last click was, and put in an if-test.

using UnityEngine;
using System.Collections;
public class destroy : MonoBehaviour {
   static float lastClickTime = -999;
    void OnMouseDown () {
        if (Time.time - lastClickTime < 1) return;   // too soon!
        lastClickTime = Time.time;
        Destroy (gameObject);
    }
}

I made lastClickTime static here because it seems like you want this delay to apply even between boxes.

1 Like

Awesome. That worked out perfectly. Thank you.

1 Like