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 MrSteve1 · Jan 15, 2014 at 02:58 PM · timermathcountdown

A Split Second Timer;

Hey Guys and Girls,

I am trying desperately now to make a split second timer, I can't seem to get the equation right for them, this is what I have developed so far.

     var timerSec = Mathf.Round(timer);
     var splitSeconds : int = (timerSec * 100) % 100;
     alertTimer = String.Format ("{0:00}:{0:00}", timerSec, splitSeconds);

This is meant to count down from 20, the "seconds" part of it works fine, but I can't make split seconds, it keeps giving me the same as "seconds".

I hope that 1) you know I mean and 2) someone can point me in the right direction.

Thanks in advance!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KellyThomas · Jan 15, 2014 at 03:15 PM

You are using your already rounded value to calculate splitSeconds.

What you want is the difference between timer and timerSec.

Assuming timer is a float measuring seconds this should work:

 var fractions: int = 100;   // for hundredths of sec
 var timerSec: int  = Mathf.RoundToInt(timer);
 var splitSeconds: int = Mathf.RoundToInt((timer - timerSec) * fractions);
 alertTimer = String.Format ("{0:00}:{0:00}", timerSec, splitSeconds);
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 JoaoOliveira · Jan 15, 2014 at 03:15 PM

You are using the round value when calculating the split seconds. Try changing to this:

 var splitSeconds : int = (timer * 100) % 100;

Also, I think that doesn't work the way you want with floats, so maybe you need this:

 var splitSeconds : int = ((int)(timer * 100)) % 100;
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 HappyMoo · Jan 15, 2014 at 10:57 PM

 "{0:00}:{1:00}"
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 HappyMoo · Jan 15, 2014 at 11:14 PM 0
Share

Also, your splitseconds should use timer, not timerSec, as timerSec is an Int already.

 var timerSec = $$anonymous$$athf.Round(timer);
     var splitSeconds : int = $$anonymous$$athf.Round(timer * 100) % 100;
     alertTimer = String.Format ("{0:00}:{1:00}", timerSec, splitSeconds);
avatar image
0

Answer by rutter · Jan 15, 2014 at 11:28 PM

If you want to track fractional seconds, why are you using an int?

 var secondsLeft : float;

 function Start() {
     secondsLeft = 20.0;
 }

 function Update() {
     secondsLeft -= Time.deltaTime;
 }

 function OnGUI() {
     GUILayout.Label(secondsLeft.ToString("n2"));
 }
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

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

Countdown when out of boundry 2 Answers

Problems in c# countdown timer 1 Answer

countdown timer starts from the begining when i reLaunch the app 1 Answer

Timer Bar Modifications 2 Answers

How do i restart my game if countdown timer runs out on a scene 3 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