• 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 /
This question was closed Oct 12, 2015 at 07:30 AM by meat5000 for the following reason:

Question Answered. And done to death.

avatar image
1
Question by thepra13 · Oct 31, 2011 at 06:29 PM ·

Time Counter

i have a simple time counter that count just the seconds

var Counter : int = 0;

function Update () {

Counter++;

guiText.text = "Time: "+Counter;

}

but i want a time that count the seconds and minutes im not couding if you helpwith the script will be helpful

Comment
Add comment · Show 2
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 CHPedersen · Oct 31, 2011 at 06:32 PM 0
Share

That snippet of code doesn't count seconds, it counts frames.

avatar image msafa · Aug 21, 2017 at 08:53 AM 0
Share

if you want to just show second (for me its survival time) you can keep it like this;

 private float surviveTime = 0.0f;
 
 void Update(){
 
     surviveTime += Time.deltaTime;
     Debug.Log(string.Format("{00:00}", surviveTime));
 
 }

5 Replies

  • Sort: 
avatar image
0

Answer by Anxo · Oct 31, 2011 at 06:58 PM

That answer is on here a lot of times. Just type "timer" in the search. What you are doing in that script is adding 1 on every frame not on every second. so if your computer is computing your script at 60 frames per second, you would have a count of 60 by the time you reach 1 second.

What you are looking for is Time.time, that counts seconds but it is a float not an int, if you search "Timer" here, you will also find how you can display that timer as an int.

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
1

Answer by Subhi · Nov 01, 2011 at 06:20 AM

var Counter = 0.000;

function Update () {

Counter += 1 * Time.deltaTime;

var Counter2 : int;

Counter2 = Counter;

guiText.text = "Time: " + Counter2;

}

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
2

Answer by RobbynB · Sep 25, 2012 at 05:16 PM

alright so here is a script I wrote to fix this issue

 var min : int;
 var sec : int;
 var fraction : int;
 var timecount : float;
 var starttime : float;
 var timeCounter : GUIText;
 
 function Start ()
 {
 starttime = Time.time;
 }
 
 function Update () {
 timecount = Time.time - starttime;
 min = (timecount/60f);
 sec = (timecount % 60f);
 fraction = ((timecount * 10) %10);
 timeCounter.text = String.Format("{00:00}:{1:00}:{2:00}",min,sec,fraction);
 }
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 Tatkowski · Mar 03, 2016 at 06:56 PM 0
Share

I've spent few hours to resolve this problem... then I found your piece. Thanks buddy :-)

avatar image
1

Answer by alattin321 · Aug 07, 2014 at 06:24 AM

 var counter : int;
 var texta : GUIText;
 function Update () {
 counter1();
 }
 function counter1 () {
 texta.text = "Time: " + counter;
 yield WaitForSeconds(1);
 counter++;
 }





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 dval · Aug 06, 2015 at 02:38 AM 0
Share

This has the advantage of being able to pause. With Time.time, it tries to catch up when you un-pause the timer.

avatar image
5

Answer by tkamruzzaman · Oct 09, 2015 at 11:04 AM

@thepra13 This code should work fine.

 private float timeRemaining; 
  void Start()
  {
      timeRemaining = 61;
  }
  void Update()
  {
      timeRemaining -= Time.deltaTime;
      if (timeRemaining > 0)
      {
          float minutes = Mathf.Floor(timeRemaining / 60);
          float seconds = Mathf.Floor(timeRemaining % 60);
          timerText.text = " " + minutes.ToString("00") + ":" + seconds.ToString("00");
      }
  }
Comment
Add comment · Show 7 · 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 meat5000 ♦ · Oct 09, 2015 at 11:09 AM 0
Share

Juuust a little late there fella ;)

avatar image tkamruzzaman meat5000 ♦ · Oct 12, 2015 at 07:28 AM 1
Share

I guess :P

avatar image martipello · Jan 20, 2016 at 11:00 PM 0
Share

but its in c

avatar image tkamruzzaman martipello · Feb 17, 2016 at 07:21 AM 0
Share

yes its in C#

avatar image baptistegr · Oct 24, 2016 at 12:05 PM 1
Share

Thanks for sharing the solution in C#

avatar image xandres95 · Aug 31, 2017 at 07:33 AM 0
Share

You are the best

avatar image ShantuApps · Feb 18, 2018 at 03:02 PM 0
Share

public int RemainingTime = 30; // Seconds void Start () { InvokeRepeating("RunTimer", 1, 1); }

 void RunTimer() {
     GameObject.text = "Show remaining time" + RemainingTime;
             RemainingTime -=1;
 }

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

All my scripts were deleted 3 Answers

How to script shooting to trigger shooting sound 1 Answer

How do I move an object in C#? 1 Answer

Can I use Unity for making non-game Apps? 5 Answers

Is it possible to make an game without having to use scripts 6 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