Search Unity

How to make a cooldown timer on a button?

Discussion in 'Scripting' started by FranticThumbs, Jul 12, 2016.

  1. FranticThumbs

    FranticThumbs

    Joined:
    Mar 5, 2015
    Posts:
    58
    Hi all
    I've been messing around trying to add a cool down timer to a button without success, so wanted to find out the best method or any pointers/tutorials to make this work correctly?

    Essentially - once the player taps this button, I would like to to disappear but have count down text replace it (so say 5 minutes until the button reappears) as a cool down.

    Thanks in advance
    G
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Easiest way is to have a cooldown button controller class that receives events from the button click and passes them on to the actual game element that you want called when the button is successfully pressed.

    Additionally, when the cooldown button controller sees that button press come through, it hides the button and starts a timer coroutine to re-enable it in the future.

    And finally it switches on a piece of co-located text saying "Button is cooling down," and that text is hidden when the button is reactivated by the above code.

    From there it is trivial to add some kind of glowing orbiting circle thingy as it "charges back up," but take it one step at a time. Each of the above steps can be added in the order I suggest them above, allowing complete testing at each step of the way.
     
    FranticThumbs likes this.
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    *Insert Panda BT Spam Here!*

    Back on topic!
    For a "cooldown" you could simply have a bool "canPress" which upon pressing the button sets this to false and starts an IEnumerator and yield waits for an amount of time that you want then setting it back to true.

    NOTE:
    For when you press the button add an if statement which checks if canPress is true and if so, continue with what you want to do.
     
    FranticThumbs likes this.
  4. FranticThumbs

    FranticThumbs

    Joined:
    Mar 5, 2015
    Posts:
    58
    thanks all, i will give these ideas a whirl