It’s way simpler ;). btw you assign hours, minutes and seconds to the same textmesh so you override the others and only the seconds will show up.
Like Ashkan said a coroutine would be the best since the time needs only an updated per sec. It’s better to update at least 2 times a sec. to avoid synchronisation interferences.
function Updatetime()
{
while(true)
{
var today = System.DateTime.Now;
textMesh.text = today.ToString("yyyy-MM-dd_HH:mm:ss");
yield WaitForSeconds(0.2f);
}
}
function Start()
{
Updatetime();
}
I don’t know what format you want but you can change the format string as you like Take a look at the this page.
This is the more efficient answer. Always be careful not to do things every frame when a much less frequent update will suffice.
If the clock only needed to update every minute, the WaitForSeconds time could be set to just a little over the time remaining until the next munite ticks over, for example.
i told to do this in my answer too. the problem of this ocde is that the variable can be defined outside the function to prevent deleting and creating it once per call.
Yes, but it first says to do something expensive in the Update() function. Feel free to edit your answer for more clarity, it's a collaboration, not a competition.
surely man! i just wanted to mention and nothing more. i think having both there is good because still many novices are afraid of coroutines and don't know how to use them.
just update the text of textMeshes or any other text element that you want to show your date/time in in Update
use something like this in your Update
function Update ()
{
textMesh.text = td.Now.Minutes.ToString();
}
i did not checked for minutes less than 0 but you can and should do it to display 09 instead of 9 as you did yourself in declarations.
now you are using only a textmesh and rewriting it’s value 3 times show it will show the seconds. use dt.Time.ToString() to get the complete time in Update or use += for minutes and seconds to concatinate strings instead of replacing them. keep in mind that these string processings are time and memory consumming so doing it in a coroutine instead of Update (for example 5 times a second) can help much. after all the time will not change 70 times a second. so the same hh:mm:ss will be computed and displayed.
function UpdateTime()
{
while(true)
{
//calculate times and update strings
yield WaitForSeconds(0.2f); //5 times a second
}
}
This is the more efficient answer. Always be careful not to do things every frame when a much less frequent update will suffice.
– WazIf the clock only needed to update every minute, the WaitForSeconds time could be set to just a little over the time remaining until the next munite ticks over, for example.
– Wazi told to do this in my answer too. the problem of this ocde is that the variable can be defined outside the function to prevent deleting and creating it once per call.
– Ashkan_gcYes, but it first says to do something expensive in the Update() function. Feel free to edit your answer for more clarity, it's a collaboration, not a competition.
– Wazsurely man! i just wanted to mention and nothing more. i think having both there is good because still many novices are afraid of coroutines and don't know how to use them.
– Ashkan_gc