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 mcconrad · Nov 08, 2014 at 02:57 PM · timercountdown

Timer script is turning minutes into seconds

the timer is changing minutes into seconds, and seconds into ms for some reason:

  function Update() {
     timer -= Time.deltaTime;
     minutes = timer / 60;
     seconds = timer % 60;
     now = String.Format("{0:0}:{1:00}", minutes, seconds);
     countdownLabel.text = "Starts in: " + now;
  }
Comment
Add comment · Show 6
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 mcconrad · Nov 08, 2014 at 02:58 PM 0
Share

also i added Time.timeScale = 1.0f; in awake() but the time is running much faster than 1 update per second...

avatar image 767_2 · Nov 08, 2014 at 03:06 PM 0
Share

update doesn't happens once per second

avatar image mcconrad · Nov 08, 2014 at 03:12 PM 0
Share

using Time.deltaTime should make the change once per second, as that is normalized time rather than Time.time which is frame-rate dependent

avatar image FairGamesProductions · Nov 08, 2014 at 03:33 PM 0
Share

No. Time.deltaTime doesn't change once per second. It changes every frame.

Time.deltaTime is: "The time in seconds it took to complete the last frame (Read Only)."

avatar image tanoshimi · Nov 08, 2014 at 03:43 PM 1
Share

@FairGamesProductions - your suggestion is worse than the original. It will display 1 $$anonymous$$ute and 5 seconds as 1:5.

Show more comments

2 Replies

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

Answer by MrSoad · Nov 08, 2014 at 03:49 PM

mcconrad, using Time.deltaTime will not make the change once per second, listen to 767_2 he is right. Time.deltaTime will give you how long it has been between this Update and the last Update, a small fraction of a second. If you want the change to only happen once every second then you should use the InvokeRepeating method(repeating once every second) instead of running this in Update.

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 tanoshimi · Nov 08, 2014 at 03:45 PM

When using UnityScript, you really really need to type your variables. Your logic for second/minute calculation only works with integer maths. Try this:

 function Update() {
     timer -= Time.deltaTime;
     var minutes : int = timer / 60;
     var seconds : int = timer % 60;
     var now : String = String.Format("{0:00}:{1:00}", minutes, seconds);
     countdownLabel.text = "Starts in: " + now;
 }
Comment
Add comment · Show 2 · 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 mcconrad · Nov 08, 2014 at 03:47 PM 0
Share

i declared all my vars outside the function. here is full:

 #pragma strict
 
 var timer: int;
 private var $$anonymous$$utes: int;
 private var seconds: int;
 private var now:  String;
 private var countdownLabel: UILabel;
 
 function Awake() {
     countdownLabel = GameObject.Find("CountdownLabel").GetComponent(UILabel);
     Time.timeScale = 1.0f;
 }
 
  function Update() {
     timer -= Time.deltaTime;
     $$anonymous$$utes = timer / 60;
     seconds = timer % 60;
     now = String.Format("{0:0}:{1:00}", $$anonymous$$utes, seconds);
      countdownLabel.text = "Starts in: " + now;
  }
  
avatar image tanoshimi · Nov 08, 2014 at 06:11 PM 1
Share

and timer needs to be a float...

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

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

Related Questions

countdown/countup timer 1 Answer

How to stop a Countdown Timer? 1 Answer

Timer script not perfectly working 1 Answer

how to stop countdown timer after move panel ? how to fix this ?,can you tell me how to stop countdown timer after move panel ? how to fix this ? 0 Answers

How can I display the GAME OVER word across the centre of the screen when the when my timer hits zero? 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