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 :: password regex asp.net 
Csharp :: c# does value exist in list 
Csharp :: How can I display image from database in asp.net mvc. I created image table and image path as varchar 
Csharp :: get mouse inpuit new input system 
Csharp :: onmousedown not working unity 
Csharp :: c# bootstrap checkbox 
Csharp :: c# write line 
Csharp :: c# only letters 
Csharp :: c# static 
Csharp :: find all factors of a given number 
Csharp :: c# .net automapper profile 
Csharp :: private Vector3 direction; 
Csharp :: Why Duplicate "..TargetFrameworkAttribute" c# assemblies created 
Csharp :: c# Intersectcase insensitive 
Csharp :: C# Calculate MD5 Checksum For A File 
Csharp :: monogame print debug 
Csharp :: display array elemetns to text box c# 
Csharp :: exe path c# 
Csharp :: change color unity over time 
Csharp :: unity datetime to string 
Csharp :: unity cannot click button 
Csharp :: unity get distance between line and point 
Csharp :: Unity upload image to project 
Csharp :: vb.net delete folder if exists 
Csharp :: 2d array rows and columns in c# 
Csharp :: c# decimal to fixed 2 
Csharp :: C# Change color 
Csharp :: unity find deactivated gameobject 
Csharp :: create class for database connection in c# 
Csharp :: change object position 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =