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 Natalievv · Feb 07, 2015 at 11:15 AM · c#timercountdown

Timer going negative...Help

Hi, I found this code here and changed it a lit bit since I wanted hours to be displayed too. I achieved this, yay, but when the timer reaches 00:00:00 instead of stopping it keeps counting but like this 00:00:0-1 and so on. Is there anything I'm not doing or doing the wrong way? Any help will be really appreciated!

using UnityEngine; using System.Collections;

public class teste : MonoBehaviour { private float _startTime; private float _restSeconds;

 private int _roundedRestSeconds;
   
 private float _displaySeconds;
 private float _displayMinutes;
 private float _displayHours;
 
 public int CountDownSeconds;
 
 private float _timeLeft;
 private string _timetext;

 // Use this for initialization 
 void Start()
 {
     _startTime = Time.deltaTime;
     
 }

 void Update()
 {
     Timer();
 }
 void OnGUI()
 {
     GUI.Label(new Rect(650, 100, 200, 40), _timetext);
    
 }
 void Timer()
 {
     _timeLeft = Time.time - _startTime;
     _restSeconds = CountDownSeconds - (_timeLeft);
     _roundedRestSeconds = Mathf.CeilToInt(_restSeconds);
     _displaySeconds = _roundedRestSeconds % 60;
     _displayMinutes = (_roundedRestSeconds / 60) % 60;
     _displayHours = (_roundedRestSeconds / 3600);
     _timetext = string.Format("{0:00}:{1:00}:", _displayHours, _displayMinutes);
     if (_displaySeconds > 9)
     {
         _timetext = _timetext + _displaySeconds.ToString();
     }
     else
     {
         _timetext = _timetext + "0" + _displaySeconds.ToString();
     }

     if (_displayHours == 0 && _displayMinutes == 0 && _displaySeconds == 0)
     {
         Debug.Log("The end!");
         
     }

  }
   

}

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

3 Replies

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

Answer by Mmmpies · Feb 07, 2015 at 11:37 AM

Set a bool let's say

 private bool useTimer = true;

Then in Update:

 if(useTimer)
     Timer();

and in your if check:

 if (_displayHours <= 0 && _displayMinutes <= 0 && _displaySeconds <= 0)
      {
          Debug.Log("The end!");
          useTimer = false;
      }
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 Bunny83 · Feb 07, 2015 at 11:29 AM

edit Sorry had it wrong in the beginning because of the naming of yoru variables ^^ "_timeLeft" isn't the time left but the time passed. It's increasing. I fixed it...

Line 31, instead of

 _restSeconds = CountDownSeconds - (_timeLeft);

do this:

 _restSeconds = Mathf.Max(0, CountDownSeconds - _timeLeft);

The Max function returns the greater value of those passed to the function. So usually (Time.time - _startTime) is larger and is returned. But when it becomes negative the "0" is greater so the 0 is returned.

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 Natalievv · Feb 08, 2015 at 01:51 AM 0
Share

Thanks! You all helped me a lot! :)

avatar image
0

Answer by NoseKills · Feb 07, 2015 at 11:38 AM

You could for example do _roundedRestSeconds = Mathf.Max(0, Mathf.CeilToInt(_restSeconds));. The source of all your time strings is _roundedRestSeconds so just make sure that never goes below 0.

Also I think it should be

 void Start()
  {
      _startTime = Time.time;   
  }

since in Update you are counting time passed from Time.time ?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Countdown timer into text c# 1 Answer

Problems in c# countdown timer 1 Answer

How do i restart my game if countdown timer runs out on a scene 3 Answers

How can I display the GAME OVER word across the centre of the screen when the when my timer hits zero? 3 Answers

how to Subtract string from TimeSpan 0 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