Cooldown button / lock button for a certain time

Hello, I would like to have a cooldown on my button so that you can’t press it for a certain time, but unfortunately you can still press it with this script, what can you do? Even if the cooldown is over, the button can be pressed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Abilities : MonoBehaviour
{
public Image abilityImage1;
public float cooldown = 5;
bool isCooldown = false;
public Button ability1;

void Start()
{
    abilityImage1.fillAmount = 0;
    ability1.onClick.AddListener(AbilityUsed);
}

private void AbilityUsed()
{
    if (isCooldown)
        return;
    isCooldown = true;
    abilityImage1.fillAmount = 1;
    StartCoroutine(LerpCooldownValue());
}

private IEnumerator LerpCooldownValue()
{
    float currentTime = 0;
    while (currentTime < cooldown)
    {
        abilityImage1.fillAmount = Mathf.Lerp(1, 0, currentTime /
cooldown);
        currentTime += Time.deltaTime;
        yield return null;
    }
    abilityImage1.fillAmount = 0;
    isCooldown = false;
}
}

If your cooldown is over, you want it to be used, don’t you?

Anyways, that being said… are you certain you don’t have more than one of these scripts targeting the same ability?

Did you add some debug messages in your while loop to spit out the value of isCooldown?

If you have pressed the button, you should no longer be able to press it for a certain period of time.

If isCooldown is true, it will return. So you need to start debugging your code to see what is going on.

Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

https://discussions.unity.com/t/821482/2

GunHeat (gunheat) spawning shooting rate of fire:

https://discussions.unity.com/t/824570/2

In general, DO NOT USE coroutines for cooldown timers.

Coroutines are NOT always an appropriate solution: know when to use them!

https://discussions.unity.com/t/857304/4

https://discussions.unity.com/t/849607/7