Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Unity C# timer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public float timeRemaining = 10;
    public bool timerIsRunning = false;
    public Text timeText;
    private void Start()
    {
        // Starts the timer automatically
        timerIsRunning = true;
    }
    void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;
            }
        }
    }
    void DisplayTime(float timeToDisplay)
    {
        timeToDisplay += 1;
        float minutes = Mathf.FloorToInt(timeToDisplay / 60); 
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);
        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}
Comment

timer unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public float timeRemaining = 10;
    public bool timerIsRunning = false;
    public Text timeText;

    private void Start()
    {
        // Starts the timer automatically
        timerIsRunning = true;
    }

    void Update()
    {
        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;
            }
        }
    }

    void DisplayTime(float timeToDisplay)
    {
        timeToDisplay += 1;

        float minutes = Mathf.FloorToInt(timeToDisplay / 60); 
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);

        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }
}Copy
Comment

PREVIOUS NEXT
Code Example
Csharp :: change scene unity 
Csharp :: xml node update attribute value c# 
Csharp :: how to update a project to cross target .net core 
Csharp :: how to find how much digits in number c# 
Csharp :: read configuration workerservice 
Csharp :: windows forms iterate through all controls 
Csharp ::  
::  
Csharp :: c# print array 
Csharp :: unity3d quaternion add 90 degrees 
Csharp :: how to cast list to observablecollection c# 
Csharp :: unity c# addition class 
:: index in foreach c# 
Csharp :: animations for pause menu 
:: oncollisionenter 
::  
:: base64 string to byte array c# 
Csharp :: gvrviewer.instance.triggered replacement unity 
Csharp ::  
Csharp ::  
:: c# list get element from end 
Csharp :: convert dto to dictionary c# 
Csharp ::  
::  
::  
Csharp :: Xamarin.Forms - How to navigate to a tabbed page child page 
Csharp :: get text component unity 
:: start command line from c# 
Csharp ::  
Csharp :: call function from another script unity 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =