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 :: c# class responsible for creating instances 
Csharp :: barcode print c# 
Csharp :: c# class reference 
Csharp :: create shortcut C# WPF 
Csharp :: ExceptionFilterAttribute exception-handler-middleware-not-catching 
Csharp :: qcombobox delegate text filter 
Csharp :: Align String with Spaces [C#] 
Csharp :: best programming language for compression ratio 
Csharp :: C#$ 
Csharp :: Transparent UserControl 
Csharp :: death transition unity 2d 
Csharp :: entity framework dynamic search 
Csharp :: BOTON PARA CAMBIAR DE VIEW ASP.NET 
Csharp :: get local position unity 
Csharp :: unity button text changes when clicked 
Csharp :: c# inline 
Csharp :: wcf service dependency injection 
Csharp :: add numbers c# 
Csharp :: vb.net delete a line from text file 
Csharp :: button Previous for picturebox c# 
Csharp :: c# reflection 
Csharp :: c# windows service .net core 
Csharp :: unity c# flip sprite 
Csharp :: c# second last element 
Csharp :: get selected rows gridcontrol devexpress 
Csharp :: how to mirror an image in vs forms 
Csharp :: 10x10 table matrix C# 
Html :: starter data jpa maven dependency 
Html :: how to make a whatsapp hyperlink html 
Html :: #ubuntu "demarrer vcs en super user" 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =