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 Major · Mar 02, 2013 at 07:38 PM · glitchclocksecondminute

Weird Problem with Clock

I am making a clock to work with my sun, and I have just lost my patients. I can not think of any reason why this script won't work. What happens is that when the seconds hit 60, they set to 0 and stay there. Then the minutes start going up at light speed, resetting themselves the way they should, and then making the hours go up the way they should. I don't have an idea what is going on, but I know that it aint right.

This is the basic clock system:

 second = Time.time;
     
     if(second >= 60)
     {
         second = 0;
         minute ++;
         
         if(minute >= 60)
         {
             minute = 0;
             hour ++;
             
             if(hour >= 24)
             {
                 hour = 0;
                 
             }
         }
     }
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

3 Replies

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

Answer by HenryStrattonFW · Mar 02, 2013 at 07:52 PM

This is because you are using Time.time. When what you want to use is Time.deltaTime (delta time is the change in time since the last update) you want to add that to your seconds (your missing a + before the = as well but that may just be a typo).

See Time.time stores the time since the app started. which once over a minute, will always be setting your second value to greater than 60 each update.

Comment
Add comment · Show 1 · 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 Major · Mar 02, 2013 at 08:21 PM 0
Share

That worked thanks!

avatar image
0

Answer by $$anonymous$$ · Mar 02, 2013 at 07:50 PM

this isn't a glitch, once the seconds are >= 60, you reset the variable second, witch is instantly changed back to Time.time when you call second = Time.time; you need to increment the seconds by one when 1 seconds passes, not directly set seconds like this: Replace second = Time.time; with

`

//c#

void Start () { InvokeRepeating("Increment", 1, 1); }

void Increment () { second += 1; }

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

Answer by Eric5h5 · Mar 02, 2013 at 07:51 PM

Think about it...Time.time is a count in seconds since the game started, so when it gets to 60 and higher, your first if statement will always be true, so you will always set "second" to 0 and always increment "minute" by one and so on. When you reset "second" to 0, that has no effect on Time.time, which will continue counting up forever.

If you want to use this particular method, you should not use Time.time, but rather increment "second" by itself. I'd recommend using InvokeRepeating with an interval of 1.0, so you can have "second" be an integer that's incremented by one every time the function is called. If you're going to use Update, then make "second" a float where you add Time.deltaTime to it every frame. (But in that case, you'd want to do "`second -= 1.0`" instead of "`second = 0`", in the interests of accuracy.)

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

11 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

Related Questions

Minute Timer Issue 1 Answer

Visual timer? Like CUT THE ROPE 2 Answers

incrementing instead of speeding up ingame clock 2 Answers

weird texture distortion 0 Answers

Mac webplayer graphic glitch 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