Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Basakot · May 26, 2016 at 02:04 PM · c#timertimer-script

Time Counter

Hi, can anyone give me please a time counter script? I have one but it don't works how i want. I want to be hours/minutes/seconds(ex: 01:07:25.43). The problem is that i want the timer to activate only when i press a button, and when the button is pressed again the timer will start from where it remained.

Here is my script:

 public Text timerText;
 private float startTime;

 // Use this for initialization
 void Start () {
     startTime = Time.time;
 
 }
 
 // Update is called once per frame
 void Update () {
     float t = Time.time - startTime;

     string hours = ((int)t / 3600).ToString ();
     string minutes = ((int)t / 60).ToString ();
     string seconds = (t % 60).ToString ("f2");

     timerText.text = hours + ":" + minutes + ":" + seconds;
     }
 }
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
1
Best Answer

Answer by Mmmpies · May 26, 2016 at 02:26 PM

You haven't reduced t by the hours when you calculate the minutes. So you'll get correct number of hours the the full time for minutes.

Try

     string hours = ((int)t / 3600).ToString ("00");
     float m = t % 3600;
     string minutes = ((int)m / 60).ToString ("00");
     string seconds = (m % 60).ToString ("00");

Not got access to Unity ATM so can't test it - sorry.

EDIT - for some reason the format screwed up on the code so I've put it as an answer for now.

Comment
Add comment · Show 3 · 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 Basakot · May 26, 2016 at 02:37 PM 0
Share

It worked, thanks. But here is the real problem for me: I want the timer to stop and play when i press a button. I tried to put If(Input.GetButtonDown... but it still plays.

avatar image Mmmpies Basakot · May 26, 2016 at 02:52 PM 0
Share

Well it depends what you're doing. In menus I set Time.timeScale to 0 but I honestly can't remember how that impacts on Time.time. Like I said I haven't got access to Unity so can't test it but try setting

 Time.timeScale = 0.0f;

When the button's pressed.

EDIT Or it might be more appropriate to have t constantly updated by adding Time.deltaTime when you want the counter updating and not when you don't.

 t += Time.deltaTime;

In Update as long as that button's not pressed.

avatar image CSharpPerls · May 27, 2020 at 08:08 AM 0
Share

When the timer reaches 1 $$anonymous$$ute tho it shows 60:00 for a fraction of second before turn in 00:59

If you want to avoid that use this ins$$anonymous$$d:

 void Update()
     {
         CalculateTime();
     }
 
 private void CalculateTime()
     {
 // Stop the timer when it reaches 0
         if (currTime <= 0)
             return;
 
         currTime = startTime - Time.time;
         float m = currTime % 3600;
 
         System.TimeSpan timeSpan = new System.TimeSpan((int)currTime  / 3600), (int)m / 60, (int)m % 60);
 
         timerText.text = timeSpan.ToString(@"hh\:mm\:ss");
     }


This I believe is cleaner too :)

avatar image
2

Answer by Toon_Werawat · May 26, 2016 at 02:49 PM

If you want to convert time to string format. Why not use TimeSpan? It way more easy!

 void Update()
 {
     System.TimeSpan result = System.TimeSpan.FromSeconds(startTime);
     System.DateTime actualResult = System.DateTime.MinValue.Add(result);
     timerText.text = actualResult.ToString("hh:mm:ss");
     //Or if you want old way.
     timerText.text = result.Hours + ":" + result.Minutes + ":" + result.Seconds;
 }

But at first I see you want time counter. You can just add bool variable for it.

 public bool timeTick;

Then at update. If you press key. You can just do this.

 void Update()
 {
     timeTick = Input.GetKey(Keycode.F);
     //If it true
     if (timeTick)
     {
         startTime = Time.time;
     }
 }
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 chriscode · Jun 09, 2019 at 07:39 PM 1
Share

Doesn't work. Your string format not valid. Need something like @"hh\:mm\:ss" or "c"

avatar image
0

Answer by Basakot · May 26, 2016 at 03:26 PM

 public Text timerText;
 private float startTime;

 // Use this for initialization
 void Start () {
     startTime = Time.time;
 
 }
 
 // Update is called once per frame
 void Update () {
     float t = Time.time - startTime;
     string hours = ((int)t / 3600).ToString ("00");
     float m = t % 3600;
     string minutes = ((int)m / 60).ToString ("00");
     string seconds = (m % 60).ToString ("f2");
     
     timerText.text = hours + ":" + minutes + ":" + seconds;
     if (Input.GetButtonDown("Right")) {
         startTime = Time.time;
     }
 }

}

@Toon_Werawat @Mmmpies this is how the scrip ended. It's exactly what i needed, but when i press the button, it starts from 0. I want to start from where it left.

Comment
Add comment · Show 3 · 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 Mmmpies · May 26, 2016 at 03:43 PM 0
Share
 startTime = Time.time;

Is fine for the first time you call it but really you need

 startTime = t;

In that button down and it'll pick up the last value of t and not the current Time.time. Struggling to run this in my head but you may need to protect t from being updated all the time as well. startTime can be set to t but the

 t = Time.time - startTime;

line runs all the time and Time.time will update. That idea of adding Time.deltaTime to t if the buttons not pressed would work though.

EDIT - in fact try this...

 void Update () {
      if (Input.GetButtonDown("Right")) {
          // Do what ever you want in here or convert the if to an if not
      } else {
      float t += Time.deltaTime;
      }

      string hours = ((int)t / 3600).ToString ("00");
      float m = t % 3600;
      string $$anonymous$$utes = ((int)m / 60).ToString ("00");
      string seconds = (m % 60).ToString ("f2");
      timerText.text = hours + ":" + $$anonymous$$utes + ":" + seconds;
      }
avatar image Basakot · May 26, 2016 at 07:25 PM 0
Share

@$$anonymous$$mmpies Thanks, i just putted startTime = t ins$$anonymous$$d on the update startTime = Time.time;. Thanks so much again!

avatar image Mmmpies Basakot · May 26, 2016 at 08:42 PM 0
Share

Hey no problem @Basakot amazed my "debug in head" worked. Technically I think adding Time.deltaTime is a better solution but really most program$$anonymous$$g, game or otherwise, is smoke and mirrors to make it look like the real world so if it "works as intended"™ then it's good enough ;¬)

avatar image
0

Answer by komal2992 · Jan 17, 2018 at 07:20 AM

Guys it's very simple. Try this one

CountDown In hours, Minutes and seconds

countDown -= Time.deltaTime;

countDownText.text = (((Mathf.Floor(countDown / 3600f)) % 60).ToString("00")) + ":" + (((Mathf.Floor(countDown / 60f)) % 60).ToString("00")) + ":" + (Mathf.Floor(countDown % 60f).ToString("00"));

@Basakot

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

166 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 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 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 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 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 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

Start and Stop Timer Help 1 Answer

I want player to be able to play again after 1 hour.,I 0 Answers

How do I add my Timer to my Score system in C# ? 0 Answers

Countdown timer... 2 Answers

Timer System in GameManager with two lists 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