- Home /
Help with clock script
I am having issues with my clock script: The problem is:
When the seconds get 60, the scripts goes crazy!
using UnityEngine; using System.Collections;
public class Clock : MonoBehaviour {
public float seconds = 0;
public int minutes = 1;
public int hours = 22;
public int day = 0;
public float month;
public float year;
public bool isBusy = false;
public dfLabel textd;
// Update is called once per frame
void Update () {
if (!isBusy){
isBusy = true;
seconds = (int) Time.time;
if (seconds <=9 && minutes <=9 && hours <=9)
{
textd.Text = "0" + hours + ":0" + minutes + ":0" + seconds;
}
else if (hours <=9 && minutes <=9)
{
textd.Text = "0" + hours + ":0" + minutes + ":" + seconds;
}
else if (minutes <=9)
{
textd.Text = hours + ":0" + minutes + ":" + seconds;
}
else if (hours <=9)
{
textd.Text = "0" + hours + ":" + minutes + ":" + seconds;
}
else
{
textd.Text = hours + ":" + minutes + ":" + seconds;
}
if (seconds == 60)
{
seconds = 0;
minutes+=1;
}
isBusy = false;
}
}
}
A better description of exactly what trouble your having would be nice.
Answer by whydoidoit · Mar 02, 2014 at 06:35 AM
This is an Update function called multiple times per frame. Because of that seconds will be == 60 for multiple frames (you setting seconds to 0 doesn't help, because you set it back to Time on the next frame).
You would be better off add Time.deltaTime to a float and checking when it exceeds 60 and then take 60 off it.
public float time = 0;
...
void Update() {
time += Time.deltaTime;
if(time >= 60) {
minutes++;
time -= 60;
}
Here's a thing I made a while back for a game clock/timer. I had it only for hours/$$anonymous$$utes/seconds, but added another method for days just now in case you or anyone else wanted those also (and in case I decided I wanted them at some point too).
STRING.cs
using UnityEngine;
using System.Collections;
public class STRING : $$anonymous$$onoBehaviour
{
public struct Format
{
public struct Time
{
//hours $$anonymous$$utes and seconds
public static string H$$anonymous$$S(float seconds)
{
char zero = '0' ;
int hours = (int) (seconds / 3600f) ;
int $$anonymous$$utes = (int) ((seconds % 3600f) / 60f) ;
int secs = (int) ((seconds % 3600f) % 60f) ;
string h = hours.ToString().PadLeft(2, zero) + ":" ;
string m = $$anonymous$$utes.ToString().PadLeft(2, zero) + ":" ;
string s = secs.ToString().PadLeft(2, zero) ;
return (h + m + s) ;
}
//days hours $$anonymous$$utes and seconds
public static string DH$$anonymous$$S(float seconds, bool padDays)
{
string s = "" ;
int days = (int)(seconds / 86400f) ;
s = padDays ? "000:" : "0:" ;
if(days <= 0)
return (s + H$$anonymous$$S(seconds)) ;
char zero = '0' ;
s = padDays ? days.ToString().PadLeft(3, zero) : days.ToString() ;
s += ":" ;
float leftOvers = (seconds % 86400f) ;
string hms = H$$anonymous$$S(leftOvers) ;
return (s + hms) ;
}
}
}
}
..Then from any other script you can do something like this...
float timer ;
void Update()
{
timer += Time.deltaTime ;
}
void OnGUI()
{
GUILayout.Label(STRING.Format.Time.H$$anonymous$$S(timer)) ;
//or
GUILayout.Label(STRING.Format.Time.DH$$anonymous$$S(timer, false)) ;
}
...etc. The code could be made "prettier" using string.Format ins$$anonymous$$d of all the padLefts, but I'm not sure how to do it that way so I just did it this way.
Posted this as a comment because @whydoidoit answered, I'm just helping a bit more.
Note : It doesn't have to be in structs like I have it, that's just how I have it because this isn't my entire STRING class or Format struct.
I didn't understand your answer whydoit, could you please help me add your script to $$anonymous$$e?
Thanks both! :D
That first script in my post, line 1-50, create a new C# Script in your project StandardAssets folder somewhere named "STRING" and copy the code into it.
Then from any other script you make you can access it the way I showed in the second code area above.
eg :
textd.Text = STRING.Format.Time.H$$anonymous$$S(yourSecondsVariable) ;
Answer by xIvan95 · Mar 03, 2014 at 08:48 AM
Thanks for the script! it works fine! But I'm trying to write my own time system and everything is working fine except for one thing:
I want the label GUI to show "00" instead of "60" when the float time gets 60. How could I do that? Thanks!
Script:
using UnityEngine; using System.Collections;
public class Clock : MonoBehaviour {
public int minutes = 1;
public int hours = 22;
public int day = 0;
public float month;
public float year;
public bool isBusy = false;
public dfLabel textd;
public float time = 0;
void Start () {
}
// Update is called once per frame
void Update () {
if (!isBusy){
isBusy = true;
//seconds = (int) Time.time;
time += Time.deltaTime;
if(time >=60) {
time -= 60;
minutes++;
}
if (time <=9 && minutes <=9 && hours <=9)
{
textd.Text = "0" + hours + ":0" + minutes + ":" + time.ToString ("00");
}
else if (hours <=9 && minutes <=9)
{
textd.Text = "0" + hours + ":0" + minutes + ":" + time.ToString ("00");
}
else if (minutes <=9)
{
textd.Text = hours + ":0" + minutes + ":" + time.ToString ("00");
}
else if (hours <=9)
{
textd.Text = "0" + hours + ":" + minutes + ":" + time.ToString ("00");
}
else
{
textd.Text = hours + ":" + minutes + ":" + time.ToString ("0");
}
isBusy = false;
}
}
}
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Increment X every one second. 2 Answers
Unity don't sign my apk android 2 Answers
How to display a list of of screens one after the next 0 Answers
Get an object to trail behind you 1 Answer