Timer.Text Code Snippet

Natasha Bibbs
Aug 29, 2017 · 1 min read

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

}

}

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade