Adding cooldown C#

Hi :smiley: I am new :smiley: And, Iโ€™m having trouble adding the cooldown to my script :frowning:
using UnityEngine;
using System.Collections;

public class TreeLogic : MonoBehaviour {

	public GameObject[] items = new GameObject[1];
	public int health = 100;

	void Update()
	{
		if(health <= 0)
		{
			Destroy(gameObject);
		}
	}

	void Hit(int damage)
	{
		Vector3 position = new Vector3((Random.Range(0, 10) - 5) + transform.position.x, Random.Range (0, 3) + transform.position.y, (Random.Range(0, 10) - 5) + transform.position.z); 

		foreach(GameObject item in items)
		{
			if(item != null)
			{
				Instantiate(item, position, Random.rotation);
			}
		}

		health -= damage;
	}
}

using UnityEngine;
using System.Collections;

public class ToolAnimator : MonoBehaviour {

	public float maxDistance;
	public float damage;
	public RaycastHit hit;

	void Update()
	{
		if(Input.GetKeyDown (KeyCode.F))
		{
			if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out hit, maxDistance))
			{
				print("Hit!");
				hit.transform.SendMessage ("Hit", damage, SendMessageOptions.DontRequireReceiver);
			}
		}
	}
}

Hi, and welcome.

Create a float that records the start of your cool down with Time.time and another that holds how long your cool down time is.

Then check if the current Time.time is greater than the start + the length.

Itโ€™s not clear what the cool down is for but letโ€™s say itโ€™s for a fire spell.

private float fireSpellStart = 0f;
private float fireSpellCooldown = 2f; // 2 = two seconds 

// then where you call the spell or whatever it is you call
// check that Time.time is greater than fireSpellStart
// + fireSpellCooldown
// and if it is allow that spell to be cast but record the start time

if (Time.time > fireSpellStart + fireSpellCooldown)
{
    fireSpellStart = Time.time;

If you want to record how long you have left the have another variable that when you start the cool down is set to fireSpellCooldown and then in update is reduced by Time.deltaTime each frame until <= 0.

Of course you can make this more complex if you have hundreds of different spells or attacks that you want to have different cool down times for but this should give you an idea of what to do.

Worth looking up Time.time and Time.deltaTime as youโ€™ll likely use them a lot.

Hope that helps.

can anybody help it doesnt work

public GameObject platform;
public float platformStart = 0f;
public float cooldown = 4f;

void Update()
{

    if (Input.GetMouseButton(0))
    {
        if (Time.time >= platformStart + cooldown)
        {
            platformStart = Time.time;
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 2.0f;
            Vector3 objectPos = Camera.current.ScreenToWorldPoint(mousePos);
            Instantiate(platform, objectPos, Quaternion.identity);

        }

Honestly, the straight up easiest and best way to do cooldowns is to use a coroutine.

public class CooldownControllerExample : MonoBehaviour
{
    public float attackCooldown;
    private bool cool = true;

    public void CooldownStart()
    {
        StartCoroutine(CooldownCoroutine());
    }
    IEnumerator CooldownCoroutine()
    {
        cool = false;
        yield return new WaitForSeconds(attackCooldown);
        cool = true;
    }
}

Whenever you would want to start the cooldown, all you would have to do if call CoolDownStart();, and be checking the bool. if bool is true, you are cool :wink: