- Home /
Question by
dandelo99 · Apr 20, 2016 at 10:53 AM ·
timer-scriptclock
How can i Set my Clock
I use this script in 3d Text it works, but i want to set clock for example it'll starts at 00:00:00 how can i do this ? this is the my script thanks a lot.
var date : System.DateTime;
//sets something allowing you to populate it with the date and time
private var text : String;
function Update()
{
//tells the var to look for the precise moment you are at
var date = System.DateTime.Now;
//converts the above var to a readable string
text = date.ToString("HH:mm:ss");
//prints it to inspector
GetComponent(TextMesh).text = text;
}
Comment
Set what clock? That actual system clock? Or, some in-game "clock"?
Answer by Jessespike · Apr 20, 2016 at 07:42 PM
Store a "start time" and subtract it from the "current time", this will give you a timespan of the elapsed time. Call the Reset function to start back at 00:00:00.
var date : System.DateTime;
//sets something allowing you to populate it with the date and time
private var text : String;
var startDateTime : System.DateTime;
function Start()
{
Reset();
}
public function Reset()
{
startDateTime = System.DateTime.Now;
}
function Update()
{
//tells the var to look for the precise moment you are at
var date = System.DateTime.Now;
//converts the above var to a readable string
//text = date.ToString("HH:mm:ss");
// calculate the timespan between the start time and current time
var timespan : System.TimeSpan = date - startDateTime;
text = String.Format("{0}:{1}:{2}",
timespan.Hours.ToString("00"),
timespan.Minutes.ToString("00"),
timespan.Seconds.ToString("00"));
//prints it to inspector
GetComponent(TextMesh).text = text;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612075224im_/https://answers.unity.com/themes/thub/images/avi.jpg)