• 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
1
Question by amincahcepu · May 30, 2011 at 08:50 PM · timer countdown

timer countdown too fast

my code for countdown is work, but when i activate it, the time twice faster from normal time

 var g :float;
 static var guiTime : float; 
 function Awake(){
     guiTime=g;
     }
 function OnGUI () {
     if (guiTime > 0){
        guiTime -=  Time.deltaTime;var minutes : int = guiTime / 60;
        var seconds : int = guiTime % 60;
        var fraction : int = (guiTime * 100) % 100;
 
        text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); 
        GUI.Label (Rect (400, 25, 100, 30), text);
     }
 }

anybody know what's wrong?

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

2 Replies

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

Answer by Antony-Blackett · May 30, 2011 at 08:59 PM

OnGUI() is called multiple times a frame so you are deducting deltaTime multiple times per frame. Move the guiTime -= Time.deltaTime; line to Update()

 var g :float;
 static var guiTime : float; 
 
 function Awake()
 {
     guiTime=g;
 }
 
 function OnGUI () {
     if (guiTime > 0){
        var minutes : int = guiTime / 60;
        var seconds : int = guiTime % 60;
        var fraction : int = (guiTime * 100) % 100;
 
        text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); 
        GUI.Label (Rect (400, 25, 100, 30), text);
     }
 }
 
 function Update()
 {
     guiTime -=  Time.deltaTime;
 }
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 aldonaletto · May 30, 2011 at 09:01 PM

Time.deltaTime is meaningful only in Update() and FixedUpdate(). Move most of your routine to Update() and let only the statement GUI.Label(...) in OnGUI().

Comment
Add comment · Show 4 · 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 Antony-Blackett · May 31, 2011 at 08:47 PM 0
Share

deltaTime is no meaningful in FixedUpdate() either. You need to use Time.fixedDeltaTime in FixedUpdate(). I think you mean LateUpdate().

avatar image Wolfram · May 31, 2011 at 09:11 PM 1
Share

However, in FixedUpdate(), deltaTime automatically maps to fixedDeltaTime, so you can still use it.

avatar image Antony-Blackett · Jun 01, 2011 at 09:54 AM 0
Share

Oh, I did not know that... what about in coroutines that wait for fixed update?

avatar image burnout156 Antony-Blackett · Feb 18, 2020 at 08:50 PM 0
Share

Time.fixedDeltaTime work for me, thanks

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

teleporting player after time runs out 2 Answers

Invoke is not working consistently 0 Answers

Unity 5 - Adding a Pre-made Countdown Timer C# Script to the Game Scene as a Text UI Element 1 Answer

How to add different countdown timer? 1 Answer

How to have a Count Down Timer carry over to a new level? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges