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 /
  • Help Room /
avatar image
4
Question by reptilebeats · Jul 10, 2012 at 03:04 AM · timer

timer stopwatch

hi i was just wandering how i would get an accurate timer to seconds?

it has to have milliseconds seconds minutes and hours, but time.time cant be used as it needs to reset sometimes back to zero. invokerepeating is good for it if i dont use milliseconds,

Comment
Add comment · Show 5
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 ByteSheep · Jul 10, 2012 at 03:12 AM 0
Share

You can use Time.deltaTime in the Update function to calculate the time that has passed since the last frame. You can then add this to the value of a variable and display the time using a gui element. Here is an example which will display $$anonymous$$utes and seconds but hours and milliseconds could also be added:

 var sec : float = 0;
 var intsec : int;
 var $$anonymous$$utes = 0;
 var seconds;
 var time;
 
 function Update() {
 
 sec = sec + Time.deltaTime;
 intsec = sec;
 
  if(intsec < 10)
  {
   seconds = "0"+intsec;
  }
  else
  {
   seconds = intsec;
  }
 
  if(intsec >= 60)
  {
   $$anonymous$$utes++;
   sec=0;
  }
 
  time = $$anonymous$$utes + ":" + seconds;
 
 }
 
 
 function OnGUI() {
 
  GUI.Label (Rect (10, 10, 100, 20), time);
 
 }
avatar image reptilebeats · Jul 10, 2012 at 03:27 AM 0
Share

it needs to be in a fixed update, well it doesnt it just makes my life simpler when pausing and stuff, ill give this a go in the morning when i can think clearly.

avatar image ByteSheep · Jul 10, 2012 at 03:30 AM 0
Share

Yeah It makes more sense to put the rest of the code into the update and just leave the GUI.Label in the OnGUI() function, but it will work the same like this since the sec variable is only being updated in the Update() function. I'll change it though.. thanks for pointing that out :)

avatar image reptilebeats · Jul 10, 2012 at 03:49 AM 0
Share

I will note im using ngui as its fir iphone, and i really like the one draw call feature without manually doing it all my self. I tend to stay away from ongui as i have heard bad things about it. Tend to use rays or hittest on gui.

avatar image robertbu · Mar 04, 2014 at 05:43 PM 0
Share

If this is for real ti$$anonymous$$g vs. game ti$$anonymous$$g, take a look at the Stopwatch class:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by freezing_ · Mar 04, 2014 at 05:46 PM

 using System.Diagnostics;
 
 public Stopwatch timer;
 
 //ctr
 timer = new StopWatch();
 
 timer.Start();
 
 // delay
 
 timer.End();
 
 use timer.Elapsed ( its a TimeSpan instance)

if you use debug in the same class it will interfere with the Debug in class in c# so just use it a class you do not Debug :)

Comment
Add comment · Show 6 · 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 idiot333 · Feb 24, 2015 at 05:28 PM 0
Share

That's what I wanted. thanks.

avatar image Martian-Games · Jun 10, 2017 at 11:05 PM 2
Share

Or you can clarify the Debug class you want to use thus:

 using System.Diagnostics;
 using Debug=UnityEngine.Debug;
avatar image Davonafe · Jan 10, 2018 at 02:29 PM 0
Share

I'm having trouble with the stopwatch class in system.diagnostics. it isn't working on builds But works in the editor are you having this issue at all?

avatar image creganjordan292 Davonafe · Jan 10, 2018 at 02:32 PM 0
Share

System.Diagnostics doesn't get included in a final build, and IIRC you need to change from .NET 2.0 Subset to the full .NET 2.0 runtime.

avatar image Davonafe creganjordan292 · Jan 12, 2018 at 07:26 PM 0
Share

i believe you are correct setting to .net did seem to work even in builds

avatar image Aristonaut · Oct 19, 2019 at 12:31 AM 0
Share

The System Diagnostics timer may cause problems if you want to target stuff other than windows. (That may change with .net core... depending on the target.)

avatar image
1

Answer by Danief_ · Jun 06, 2019 at 06:02 PM

If anybody else comes across this and doesn't want to use the diagnostics one, here's a custom class I wrote.

 //Written by Daniel Keele - 6/6/2019
  using System;
  using UnityEngine;
  
  public class Stopwatch : MonoBehaviour
  {
      private float elapsedRunningTime = 0f;
      private float runningStartTime = 0f;
      private float pauseStartTime = 0f;
      private float elapsedPausedTime = 0f;
      private float totalElapsedPausedTime = 0f;
      private bool running = false;
      private bool paused = false;
      
      void Update()
      {
          if (running)
          {
              elapsedRunningTime = Time.time - runningStartTime - totalElapsedPausedTime;
          }
          else if (paused)
          {
              elapsedPausedTime = Time.time - pauseStartTime;
          }
      }
  
      public void Begin()
      {
          if (!running && !paused)
          {
              runningStartTime = Time.time;
              running = true;
          }
      }
  
      public void Pause()
      {
          if (running && !paused)
          {
              running = false;
              pauseStartTime = Time.time;
              paused = true;
          }
      }
  
      public void Unpause()
      {
          if (!running && paused)
          {
              totalElapsedPausedTime += elapsedPausedTime;
              running = true;
              paused = false;
          }
      }
  
      public void Reset()
      {
          elapsedRunningTime = 0f;
          runningStartTime = 0f;
          pauseStartTime = 0f;
          elapsedPausedTime = 0f;
          totalElapsedPausedTime = 0f;
          running = false;
          paused = false;
      }
  
      public int GetMinutes()
      {
          return (int)(elapsedRunningTime / 60f);
      }
  
      public int GetSeconds()
      {
          return (int)(elapsedRunningTime);
      }
  
      public float GetMilliseconds()
      {
          return (float)(elapsedRunningTime - System.Math.Truncate(elapsedRunningTime));
      }
 
      public float GetRawElapsedTime()
      {
          return elapsedRunningTime;
      }
  }
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 Aristonaut · Oct 19, 2019 at 12:16 AM 0
Share

Note on limitation of this answer: This won't work for anyone needing times updated between FixedUpdate and you can't just change Update to FixedUpdate, cuz Time.time represents the time of the start of the frame. Also, due to Time.time being a float, the resolution of Time.time is only accurate to the nearest second for 97 days. $$anonymous$$illiseconds, as the OP asks for, will be lost even sooner.

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

Simple Timer 3 Answers

Resetting the scale of an object being scaled by a timer 1 Answer

Timer Countdown Pop Up Screen Before Scene "Starts" Please Help!! 2 Answers

Problem with a stopwatch 0 Answers

Timer.deltaTime resetting once and only once instead of whenever I want. 2 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