Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Cooldown button / lock button for a certain time

Discussion in 'Scripting' started by gamepvp30, Dec 3, 2022.

  1. gamepvp30

    gamepvp30

    Joined:
    Jun 24, 2020
    Posts:
    4
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Abilities : MonoBehaviour
    7. {
    8. public Image abilityImage1;
    9. public float cooldown = 5;
    10. bool isCooldown = false;
    11. public Button ability1;
    12.  
    13. void Start()
    14. {
    15.     abilityImage1.fillAmount = 0;
    16.     ability1.onClick.AddListener(AbilityUsed);
    17. }
    18.  
    19. private void AbilityUsed()
    20. {
    21.     if (isCooldown)
    22.         return;
    23.     isCooldown = true;
    24.     abilityImage1.fillAmount = 1;
    25.     StartCoroutine(LerpCooldownValue());
    26. }
    27.  
    28. private IEnumerator LerpCooldownValue()
    29. {
    30.     float currentTime = 0;
    31.     while (currentTime < cooldown)
    32.     {
    33.         abilityImage1.fillAmount = Mathf.Lerp(1, 0, currentTime /
    34. cooldown);
    35.         currentTime += Time.deltaTime;
    36.         yield return null;
    37.     }
    38.     abilityImage1.fillAmount = 0;
    39.     isCooldown = false;
    40. }
    41. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    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?
     
  3. gamepvp30

    gamepvp30

    Joined:
    Jun 24, 2020
    Posts:
    4
    If you have pressed the button, you should no longer be able to press it for a certain period of time.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    If isCooldown is true, it will return. So you need to start debugging your code to see what is going on.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297

    GunHeat (gunheat) spawning shooting rate of fire:

    https://forum.unity.com/threads/spawning-after-amount-of-time-without-spamming.1039618/#post-6729841

    In general, DO NOT USE coroutines for cooldown timers.

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

    https://forum.unity.com/threads/things-to-check-before-starting-a-coroutine.1177055/#post-7538972

    https://forum.unity.com/threads/heartbeat-courutine-fx.1146062/#post-7358312