Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 02, 2014 at 02:18 AM · clock

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;
  
 }
  
 }
  
  
 }
Comment
Add comment · Show 1
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 mattyman174 · Mar 02, 2014 at 06:31 AM 0
Share

A better description of exactly what trouble your having would be nice.

2 Replies

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

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;
       }
Comment
Add comment · Show 4 · 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 Lo0NuhtiK · Mar 02, 2014 at 07:30 AM 1
Share

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.

avatar image whydoidoit · Mar 02, 2014 at 07:49 AM 0
Share
  • Nice one

avatar image xIvan95 · Mar 02, 2014 at 07:18 PM 0
Share

I didn't understand your answer whydoit, could you please help me add your script to $$anonymous$$e?

Thanks both! :D

avatar image Lo0NuhtiK · Mar 02, 2014 at 08:16 PM 0
Share

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) ;
avatar image
0

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;

     }

     }


 }
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.

Follow this Question

Answers Answers and Comments

23 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 avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges