• 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 /
This question was closed Feb 02, 2015 at 07:05 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Hitman666 · Feb 02, 2015 at 03:44 AM · timersynchronization

Two timers out of sync

I made a test project which you can clone on Github, and documented my issue here.

In essence the problem is that I have two timers; one that counts down from 70 seconds to 0 (and then starts all over again), and the second one just shows the current client time.

Problems which I have are the following:

  1. If you open up any other window you will notice that the timer will stop, and continue counting once you return to the Unity window

  2. TimerText and TimeText are not “ticking off” at the same time

If someone has hints on how to approach solving this, I would be very grateful.

For reference, my code is this:

 #pragma strict
 
 var timer: float = 70;
 var isFinishedLevel : boolean = false;
 public var displayText : UnityEngine.UI.Text;
 public var timeText : UnityEngine.UI.Text;
 
 private var oldTimer : float;
 
 function Start(){
     oldTimer = timer;
 }
 
 function Update()
 {
     if (!isFinishedLevel) {
         timer -= Time.deltaTime;
     } 
 
     if (timer > 0) {
         var minsDisplay : String = parseInt( timer / 60 ).ToString();
         var secsDisplay : String = parseInt( timer ).ToString();
          
         if ( (timer - ( parseInt(minsDisplay) * 60)) > 10 ) {
              secsDisplay = parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
         } 
         else {
             secsDisplay = "0" + parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
         }
         
         displayText.text = minsDisplay + " : " + secsDisplay;
     } 
     else {
          timer = oldTimer;
     }
     
     CurrentTime();
 }
 
 function CurrentTime() { 
     var dt : System.DateTime = System.DateTime.Now;
     var h : int = dt.Hour; 
     var m : int = dt.Minute; 
     var s : int = dt.Second;
 
     timeText.text = h + ":" + m + ":" + s;
 }


Comment

People who like this

0 Show 7
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 meat5000 · Feb 01, 2015 at 04:02 PM 0
Share

Number 1 is intended behaviour.

For 2 I only see TimeText

avatar image Hitman666 · Feb 01, 2015 at 08:40 PM 0
Share

@meat5000: Thanks - do you know how do I get around it then? Someone must have stumbled upon a similar problem, but can't seem to find it.

avatar image Hitman666 · Feb 01, 2015 at 09:22 PM 0
Share

Great, thank you!, please put this as an answer so I can accept it. Oh, and if you don't mind, can you shed some light on #2?

avatar image meat5000 · Feb 02, 2015 at 03:45 AM 0
Share

I don't see TimerText in your script :)

Did you mean displayText?

avatar image Hitman666 · Feb 02, 2015 at 06:21 AM 0
Share

Yes, the thing is that I named the two Text UI elements "TimerText" and "TimeText" in the Hierarchy. True, in the code they are called "displayText" and "timeText". And, true, that's not nice to have them called separately.

Nevertheless, do you know how to make them "tick" in the same exact time?

Show more comments

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by meat5000 · Feb 02, 2015 at 02:10 PM

timeText is updated with system time. If you want them to tick the same, displayText needs to update with a trigger from that.

The text 'tickrate' is the same. They are simply receiving new information at different times.

Comment
Hitman666

People who like this

1 Show 10 · 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 Bunny83 · Feb 02, 2015 at 12:55 PM 1
Share

Part 2)

Another problem is that you cut off fractional seconds when the 70 seconds are passed. Instead of

 timer = oldTimer;

do

 timer += oldTimer;

This will preserve the fractional part from the last interval.

avatar image meat5000 Bunny83 · Feb 01, 2015 at 08:57 PM 1
Share

Part 1)

Edit -> Project Settings -> Player -> Run In Background

avatar image meat5000 Bunny83 · Feb 02, 2015 at 01:49 PM 0
Share

Indeed! Fractions of a second could lead to several frames of difference.

avatar image Hitman666 Bunny83 · Feb 02, 2015 at 05:26 PM 1
Share

Excellent, thank you very much for your help!, will upvote once I get over the freakish 15 rep treshhold. It's kind of weird to come here from StackOverflow (where I got quite a profile :)) and start "all over again"

Show more comments
avatar image meat5000 · Feb 02, 2015 at 05:54 PM 0
Share

There ya go :) Got you started.

avatar image Hitman666 · Feb 02, 2015 at 08:54 PM 0
Share

Thanks - upvoted as promised ;)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How I can implement a multiplayer chess clock C#? 0 Answers

is it cheaper to run timer another, or refer to other timer? 1 Answer

How do I stop my timer when all of the "dummies" are destroyed? 2 Answers

Referencing other scripts: -5 seconds rule on timer 1 Answer

PlayerPrefs & Time.time 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