How do I add a cooldown?

if you need better precision, you may need some special libraries, but will do... just add it to an empty game object, and to start a cooldown click "start cooldown" bool on the component. You can adjust the timer by setting the int value on the component in milliseconds.

using UnityEngine; using System.Collections;

public class cooldownTimer : MonoBehaviour { public int cooldownTime = 1000; //ms private bool _isCoroutineRunning = false; public bool _startCooldown = false;

// Use this for initialization
void Start()
{
    _startCooldown = false;
    _isCoroutineRunning = false;
}

// Update is called once per frame
void Update()
{
    if (_startCooldown && !_isCoroutineRunning)
    {
        _startCooldown = false;
        StartCoroutine(TrackCooldown());
    }
}

IEnumerator TrackCooldown()
{
    _isCoroutineRunning = true;
    bool _isComplete = false;
    float _timeDiff = 0f;

    while(!_isComplete)
    {
        if((_timeDiff * 1000) > cooldownTime) //~ms equivalent
        {
            _isComplete = true;
        }else
        {
            _timeDiff += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
    }

    //do stuff for cooldown event
    Debug.Log("cooldown exact time: " + (_timeDiff * 1000) + "ms");
    _isCoroutineRunning = false;
}

}

/r/Unity3D Thread