Search Unity

Loop Timer

Discussion in 'Scripting' started by Blar321, Aug 30, 2015.

  1. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    Hello everybody! I need to create a simple timer that loops so that every 20 seconds a function executes. I know this is a fairly simple task, but I have never done it before and have no idea what to do. Thanks so much!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    InvokeRepeating
     
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Coming to the forums for help is absolutely fine, but you'll find our assistance much more useful once you have a firm grasp on the basics. Take some unity tutorials, some programming tutorials and try to do a couple of things on your own. You'll soon find that a task as simple as this is a breeze. Besides, learning programming is fun!

    As BoredMormon said, you could use InvokeRepeating. Or you could build your own component.
    Here's some code. There's a bunch of different lessons at work in here, try to turn it into a learning exercise:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. /// <summary>
    5. /// The Timer component executes a custom event every set interval.
    6. /// Timer can loop.
    7. /// </summary>
    8. public class Timer : MonoBehaviour {
    9.  
    10.     //Regions are handy ways to seperate your code into sections.
    11.     #region Inspector
    12.  
    13.     //[SerializeField] is what makes fields editable in the inspector. Look up Unity serialization for more information.
    14.     //Any public field that CAN be serialized is serialized by default. We don't want the interval property to be public,
    15.     //because if other scripts or code are going to change the interval property at runtime, we want to be able to validate it.
    16.     //See the OnValidate method and Interval property below.
    17.     [SerializeField] float interval = 1f;
    18.  
    19.     public bool destroyOnExpire = false;
    20.  
    21.     //A unity event is something that can be set in the inspector or programatically.
    22.     //You can tell an event to run one function, many, or none. Learn about them because
    23.     //they are extremely handly.
    24.     public UnityEvent onExpire;
    25.  
    26.     #endregion
    27.  
    28.     #region Runtime Data
    29.  
    30.     float remaining;
    31.  
    32.     #endregion
    33.  
    34.     #region Unity Callers
    35.  
    36.     //OnValidate is called when changes are made in the inspector.
    37.     //We don't want the interval to be below zero, so if a user
    38.     //sets it as such, we fix it here.
    39.     void OnValidate()
    40.     {
    41.         if (interval < 0f)
    42.             interval = 0f;
    43.     }
    44.  
    45.     void Start()
    46.     {
    47.         remaining = interval;
    48.     }
    49.  
    50.     void Update()
    51.     {
    52.         remaining -= Time.deltaTime;
    53.  
    54.         if (remaining > 0f)
    55.             return;
    56.  
    57.         onExpire.Invoke ();
    58.  
    59.         //Maybe a timer is only intended to execute once. If so, we'll disable it to ensure the update function
    60.         //wont run again next frame, and we'll Destroy the component.
    61.         if (destroyOnExpire) {
    62.             enabled = false;
    63.             Destroy (this);
    64.  
    65.         } else     //Otherwise we reset the amount remaining, and continue as normal
    66.             remaining = interval;
    67.     }
    68.  
    69.     #endregion
    70.  
    71.     #region API
    72.  
    73.     public float Interval {
    74.         get {
    75.             return interval;
    76.         }
    77.         set {
    78.             interval = value;
    79.  
    80.             //Call OnValidate manually during runtime.
    81.             OnValidate();
    82.         }
    83.     }
    84.  
    85.     public float Remaining {
    86.         get {
    87.             return remaining < 0f ? 0f : remaining;
    88.         }
    89.     }
    90.  
    91.     //We want to be able to pause the timer by disabling it. If a component is 'enabled' it will run
    92.     //all of it's Unity-nested callers, like Update, Start, LateUpdate, etcetera. If you disable a timer, it
    93.     //will be paused weather or not you call the pause function, disable the component,
    94.     //disable the gameObject or disable the gameObjects parent, ect. As such, the following are just convenience
    95.     //properties and methods. They're the same as changing the 'enabled' member.
    96.     public bool Paused {
    97.         get {
    98.             return !enabled;
    99.         }
    100.     }
    101.  
    102.     public void Pause()
    103.     {
    104.         if (Paused)
    105.             Debug.LogWarning ("Timer on " + name + " is already paused.");
    106.  
    107.         enabled = false;
    108.     }
    109.    
    110.     public void Unpause()
    111.     {
    112.         if (!Paused)
    113.             Debug.LogWarning ("Timer on " + name + " is already running.");
    114.        
    115.         enabled = true;
    116.     }
    117.  
    118.     //This is a convienience method that we can use to create timers quickly. A UnityAction can either be a method
    119.     //or a lambda expression. Add Lambdas to your list of things to learn about, they're very handy.
    120.     public static Timer Create(GameObject gameObject, float interval, UnityAction onExpire, bool destroyOnExpire = false)
    121.     {
    122.         var timer = gameObject.AddComponent<Timer> ();
    123.         timer.interval = interval;
    124.         timer.destroyOnExpire = destroyOnExpire;
    125.  
    126.         timer.onExpire = new UnityEvent ();
    127.         timer.onExpire.AddListener (onExpire);
    128.  
    129.         return timer;
    130.     }
    131.  
    132.     #endregion
    133.  
    134. }
    135.  
    So you add this component in the editor, subscribe a bunch of methods to it's event, or you can also create it easily at runtime:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bomb : MonoBehaviour {
    5.  
    6.     void Start () {
    7.         Timer.Create (this.gameObject, 10f, Explode);
    8.     }
    9.    
    10.     void Explode()
    11.     {
    12.         Destroy (gameObject);
    13.         Debug.Log ("KABLOOIE!");
    14.     }
    15. }
    16.