• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xIvan95 · Mar 04, 2014 at 03:09 AM · timelimitsetclock

Set limit to 60 seconds

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! (I've tried things like Mathf.Clamp)

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;
  
 }
  
 }
  
  
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by danielskovli · Mar 04, 2014 at 03:18 AM

This would probably be better solved with the TimeSpan object, but you could get away with something like this as well:

 // assuming your `time` variable keeps track of seconds
 float seconds = time % 60;
 float minutes = time / 60;
 float hours = minutes / 60;
 minutes = minutes % 60;
 hours = hours % 24;
 
 Debug.Log(hours.ToString("D2") + ":" + minutes.ToString("D2") + ":" + seconds.ToString("D2"));


Update to explain the code a bit:

The first line will divide the whole big number by 60 (as in seconds per minute) and keep the remainder. That's what the modulo operator does - it gives you the remainder of a division (ie. the decimal points).

The second line will simply get the seconds converted to minutes (straight up division by 60).

The third line gets the hours - 60 minutes in every hour.

The fourth and fifth line make sure that minutes never surpass 59 and that hours don't go beyond 23.

This all means that your clock will effectively reset to 0 after 24 hours, but I'm guessing that's not a huge problem? The TimeSpan object, as mentioned, will definitely make your life easier when doing these sorts of things - and doesn't have any logical issues or limitations like my code above :)

As for the .ToString("D2") process, that's just a habit really. From reading this thread, you are probably better off using .ToString("00") - which takes ints and floats without any fuss.

In regards to how you implement it in your code, that's kind of up to you. Give it a try and ask again if you're having any specific issues :-)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image danielskovli · Mar 04, 2014 at 03:28 AM 0
Share

Oh by the way, you might need to cast the floats to ints (or round them or whatever you want) before you can use that string formatting. Not entirely sure though...

avatar image xIvan95 · Mar 04, 2014 at 03:47 AM 0
Share

So how do I add your script to mine? Also, could you please try to explain the code a little bit? I don't understand the "%" and "D2" there :/

Thanks!

avatar image danielskovli · Mar 04, 2014 at 04:07 AM 0
Share

No worries, hope that's a bit better. Let me know how you go

avatar image xIvan95 · Mar 05, 2014 at 04:47 AM 0
Share

It is not working :/ Also, when the seconds get 30, 1 minute is added :S:S help please! and thanks for your support ^^

using UnityEngine; using System.Collections;

public class Clock : $$anonymous$$onoBehaviour {

 public float seconds;
 public float minutes;
 public float hours;
 public float day;
 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;


         time += Time.deltaTime;
         seconds = time % 60;
         minutes = time / 60;
         minutes = minutes % 60;
         hours = hours % 24;


         Debug.Log(hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00"));



         /*
             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;

     }

     }


 }
avatar image danielskovli · Mar 05, 2014 at 05:50 AM 0
Share

Try flooring the variables before outputting, it's just a rounding issue. Also, you forgot to assign the hours variable properly before using modulus on it.

Try this:

 time += Time.deltaTime;
 seconds = time % 60;
 minutes = time / 60;
 hours = minutes / 60;
 minutes = minutes % 60;
 hours = hours % 24;
 
 Debug.Log($$anonymous$$athf.Floor(hours).ToString("00") + ":" + $$anonymous$$athf.Floor(minutes).ToString("00") + ":" + $$anonymous$$athf.Floor(seconds).ToString("00"));

If you're testing, it might be handy to speed through the time by assigning your time variable slightly differently. Something like this:

 time += Time.deltaTime * 100;
Show more comments
avatar image
0

Answer by OSG · Mar 05, 2014 at 05:36 AM

If it will help you, here is my timer:

     void Update () {
             
             if(timerOn) {
                 sec += Time.deltaTime;
                 System.TimeSpan ts = new System.TimeSpan(0, 0, (int)sec);
                 time.text = ts.Hours.ToString("00")+":"+ts.Minutes.ToString("00")+":"+ts.Seconds.ToString("00");
             }
 }

This generates timer like 00:00:00.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Single Step (pause, resume), or externally clock game loop 0 Answers

clock second smooth movement 1 Answer

Keep score after restart & limit ads/hour 1 Answer

Limited Energy Regeneration 3 Answers

How do you display the Time? (as in you're PCs clock) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges