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 supersun · Aug 21, 2014 at 06:24 AM · javascriptcoroutineclock

Increment X every one second.

I have a program where every tick of a clock there is a corresponding amount of time. Here for every 1 minute it is equivalent to 5 hours (simulated time), but I have a hard time where the problem exist.

 function count_coroutine()
 {
     var seconds : int = 0;
     var second_sec : int = 0;
     var second_min : int = 0;
     var second_hour : int = 0;
     while(true)
     {
         
         for (var timerz : float = 0; timerz < 1; timerz += Time.deltaTime)
         {
             second_sec += (300/60);
             if (second_sec >= 60)
             {
                 second_min++;
                 
                 second_sec -= 60;
                 
                 if (second_min >= 60)
                 {
                     second_hour++;
                     
                     second_min -= 60;
                     
                 }
                 
             }
             
             yield null;
         }
         
         seconds++;
         
         textMeshReal.text = seconds.ToString("00");
         textMeshSim.text = second_hour.ToString("00") + " : " + second_min.ToString("00") + " : " + second_sec.ToString("00");
         
     }    
 }
Comment
Add comment · Show 5
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 Unity Ninja · Aug 21, 2014 at 09:21 AM 0
Share

what is the problem?

avatar image supersun · Aug 21, 2014 at 09:46 AM 0
Share

The expected output is 1 $$anonymous$$inute (text$$anonymous$$eshReal.text) = 5 Hours (text$$anonymous$$eshSim.text), but the output is 1 $$anonymous$$inute = 5:04:55

avatar image Andres-Fernandez · Aug 21, 2014 at 10:27 AM 0
Share

The problem might be that you are adding and substracting to three different variables (I haven't checked if your math is correct). Why don't you just increment the seconds and figure out the $$anonymous$$utes and hours from them?

Besides, do you know that yield can be used with a time gap? You can use seconds. Check here.

avatar image $$anonymous$$ · Aug 21, 2014 at 10:58 AM 0
Share

In this code, no overflow in deltaTime is being carried over from the previous iteration.

avatar image supersun · Aug 21, 2014 at 11:11 AM 0
Share

@$$anonymous$$: so what do you mean? can you please explain further because I'm still a noob at this things.

2 Replies

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

Answer by $$anonymous$$ · Aug 21, 2014 at 11:46 AM

A simple solution would be to update a variable of time passed given Time.deltaTime.

 var hoursPassed : float = 0
 
 function update()
 {
     hoursPassed += Time.deltaTime * 50 / 60;
 }

That is, 50 hours pass for every 60 seconds. Sorry if the js, has a typo or w/e, I'm a C# programmer.

If you want to update only once a second, check out alishka's answer.

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 supersun · Aug 21, 2014 at 02:09 PM 0
Share

@$$anonymous$$: is hoursPassed += Time.deltaTime * 50 / 60; is equal to "1 $$anonymous$$inute = 50 Hours"?

avatar image
0

Answer by alishka · Aug 21, 2014 at 10:33 AM

You need to use waitforseconds() in the yield statement and get rid of the for loop.

Comment
Add comment · Show 3 · 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 supersun · Aug 21, 2014 at 10:49 AM 0
Share

i used waitforseconds(1); and the output is 1 $$anonymous$$ute = 00: 05: 00. The OUTPUT $$anonymous$$UST be 1 $$anonymous$$ute = 05 : 00 : 00.

avatar image alishka · Aug 21, 2014 at 11:05 AM 0
Share

Almost there ! Now you might want to change the way you increment the game time :) You waited 1 second and the game time got incremented by 5 $$anonymous$$utes.

Off the back of my head I have this:

 while(true){
 
    //wait 60 sec = 1 $$anonymous$$
    yield return new waitforseconds(60);
 
    //increment
    gametimehours += 5;
 }

avatar image supersun · Aug 21, 2014 at 11:31 AM 0
Share

but i want in every 1 second an update (per second update). Like 00:01 = 00:00:05 00:02 = 00:00:10

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

changing a coroutine's argument at the coroutine's runtime? 1 Answer

Keeping track of time? 4 Answers

Updating a TextMesh buggy? 1 Answer

A node in a childnode? 1 Answer

Help scrolling puzzle is not. 0 Answers


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