Timer.Text Code Snippet

In this code snippet I made a timer for my Roll A Ball.

using System.Collections;

using UnityEngine.UI;

using UnityEngine;

//libraries of the class, UnityEngine.UI added in

public class Timer : MonoBehaviour// Name of the script/ class

{

public Text Timertext; // Created a public text variable named Timer.text

private float startTime; // Declares a startTime/ float also meaning fractional number

void Start()

{

startTime = Time.time;// allows Timer to start at the very beginning, shows time since play is activated

}

void Update()

{

float t = Time.time — startTime; // displays time from when the timer first started

string minutes = ((int)t / 60).ToString(); // creates a string that will represent the minutes on the timer/ (int) indicating a whole number.

string seconds = (t % 60).ToString(“f2”); // f2 allowing number of two digits after decimals

Timertext.text = minutes + “:” + seconds; // prints timer to show minutes and seconds format

}

}

Like what you read? Give Natasha Bibbs a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.