Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Apiweb · Oct 10, 2012 at 12:05 AM · c#guitimercountdown

Problems in c# countdown timer

Hello guys okay?

I'm using the code below to display the counter, the counter starts with 120 seconds and decays. But when I give my scene in the play, the counter begins: 2:59. When he arrives at 2:30, it jumps to 1:29, when he arrives at 1:00, the counter is showing 1:59 and finally when he arrives at 1:30 to 0:29 he jumps and continues to give count normally.

I wonder what is wrong with my script, below:

     using UnityEngine;
     using System.Collections;
     
     public class GameManager : MonoBehaviour {
         
         private float theTimer = 0.0f;
         public float theStartTimer = 120.0f;
         float displayMinutes;
         float displaySeconds;
         
         void Start () {
             theTimer = theStartTimer;
         }
         
         void Update () {
             theTimer -= Time.deltaTime;
             
             if (theTimer <= 0) {
                 Debug.Log("Over Time");
                 theTimer = 0;
             }
             
             if (Input.GetKeyUp(KeyCode.G)) {
                 Debug.Log("Reset time");
                 theTimer = theStartTimer;
                 showRemaining = false;
             }
         }
         
         void OnGUI () {
             displayMinutes = Mathf.CeilToInt(theTimer) / 60.0f;
             displaySeconds = Mathf.CeilToInt(theTimer) % 60.0f;
             string text = string.Format("{0:00}:{1:00}", displayMinutes.ToString("0"), displaySeconds.ToString("0"));
             GUI.Label(new Rect(10, 110, 100, 20), "Time: " + text);
         }
     }
     

Remembering that it compiles and runs normally, only the display of the time you're crazy.

Comment
Add comment · Show 3
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 AlucardJay · Oct 10, 2012 at 12:09 AM 0
Share

ins$$anonymous$$d of $$anonymous$$athf.CeilToInt, try :

 $$anonymous$$athf.FloorToInt
avatar image Apiweb · Oct 10, 2012 at 12:53 AM 0
Share

Thank you, works great.

avatar image Fattie · Oct 10, 2012 at 07:17 AM 0
Share

just FWIW this sort of thing is incredibly simple using InvokeRepeating, two lines of code all round

1 Reply

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

Answer by kmeboe · Oct 10, 2012 at 12:19 AM

You need to do two things:

  • use FloorToInt (as alucardj suggested)

  • Do the floor AFTER you do the division or mod operation.

Here is the updated code:

         void OnGUI () {
            displayMinutes = Mathf.FloorToInt(theTimer / 60.0f);
            displaySeconds = Mathf.FloorToInt(theTimer % 60.0f);
            // the rest
         }
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 raviranjanjha93 · Mar 24, 2015 at 10:57 AM 0
Share

thanks.. it works

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

11 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

Related Questions

C# countdown timer 9 Answers

timer problem 1 Answer

How to reset timer? (C#) 3 Answers

Timer doesn't work properly 2 Answers

Making a Timer Out out of 3D Text using C#. 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