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. Dismiss Notice

Unity Cooldown Tutorial

Discussion in 'Scripting' started by xjjon, Aug 15, 2016.

  1. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    587
    I wrote another tutorial targeted towards beginners in Unity. In this short tutorial we will make a easy to use cooldown timer that can be used for things such as abilities, spawners, and much more. We will also make a small project to see the timer in action.



    There is a github project available here and you can view the tutorial here (in text!).

    Just want to help contribute to the Unity community, any suggestions and comments are always appreciated. Hope this helps someone!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    Some useful additions to this script would be to have a public method to start it all off:

    Code (csharp):
    1. public void ActivateCooldown();
    Then you could also have some public events, like so:

    Code (csharp):
    1. public Event NotifyNotReady;
    2.  public Event NotifyReady;
    That first event would fire if you attempted to call ActivateCooldown() before it was ready, and the second one could be called the moment the cooldown timer expires and the end user actually COULD call the ActivateCooldown() method again.

    The first event could be used to make a "Bzzzt!" negative sound, and the second event could be used to turn a button back on and/or make a "ding! your spell is ready!" sound.

    And by using events, it makes it so that game behavior can be changed directly from the Unity editor!
     
    xjjon likes this.
  3. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    587
    Thanks for reading and for the comments! Both are very useful suggestions.

    The cooldown can be toggled through the bool, however it would be useful to have an activate function to add any other initialization work that needs to be done.

    I didn't add anything about Events since this was mostly targeted for beginners, but I will add a little about them to the end, since they are very useful but may be out of scope for people just getting started.