Skip to main content

Unity - Timer

·116 words
icysamon
Author
icysamon
Electronics & Creator
Table of Contents

Time.time
#

private float timeStart; // Time recorded when the timer starts


private void Start()
{
    timeStart = Time.time; // Set the timer start time to the elapsed time since the game started
}


private void Update()
{
    if (Time.time - timeStart > 1f) // When 1 second has elapsed
    {
        timeStart = Time.time; // Reset the timer
        // Write your function here

        // End of your function
    }
}

Coroutine
#

private void Start()
{
    StartCoroutine(Timer()); // Start the coroutine
}


private IEnumerator Timer() // Define the coroutine
{
    yield return new WaitForSeconds(1f); // Delay for 1 second
    // Write your function here

    // End of your function
    yield break; // End the coroutine
}