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 :: get string name of dropdown in unity 
Csharp :: when do i need to end a sentence with ; in c# 
Csharp :: search the third word in string in c# 
Csharp :: change a dropdown to a specific option via script 
Csharp :: check c# date for 0001/01/01 
Csharp :: c# read all text from a file 
Csharp :: C# multiple button click event to password textbox 
Csharp :: how to stop player rotating when hit by object 
Csharp :: reference to another script unity 
Csharp :: xamarin timer example 
Csharp :: how to make a partical system to destroy itself after it finishing 
Csharp :: how to generate random letters in C# 
Csharp :: avoid autocomplete input text asp.net 
Csharp :: object list to csv c# 
Csharp :: SIMPLE HTTP REQUEST C# 
Csharp :: save file dialog filter c# 
Csharp :: learn c# 
Csharp :: unity access phone camera 
Csharp :: how to get the path of the current directory in c# 
Csharp :: unique id c# 
Csharp :: how to move mouse in c# 
Csharp :: c# string to hex 
Csharp :: C# .NET Core linq Distinct 
Csharp :: c# how to terminate console application 
Csharp :: import time C# 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =