• 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
Question by Bryangs · Nov 28, 2016 at 08:09 PM · timer-scriptsecondsclockformattingminutes

Minutes and Seconds in a text

Hey guys, i am making a CountDown Timer, and it work, but not as i wanted. I made it very simple in C#. It's basically a float that is changed over time, and it is shown in a text. If i set the float to 900 (15 minutes), then the text is going to be shown like 900 normally. What i want to do is, if i set the float to 900, then in the text, it will show something like 15:00. How would i do that? I have no idea of how to start.

This is my script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour
 {
     public float timeLeft = 900.0f;
     public Text text;
     bool clock;
     private float mins;
     private float secs;
 
     void Update()
     {
         if (timeLeft > 0 && clock == false)
         {
             clock = true;
             StartCoroutine(Wait());
         }
 
     }
 
     IEnumerator Wait()
     {
         timeLeft -= 1;
         UpdateTimer();
         yield return new WaitForSeconds(1);
         clock = false;
     }
 
     void UpdateTimer()
     {
         text.GetComponent<UnityEngine.UI.Text>().text = timeLeft.ToString();
     }
 }

Could someone help me to change the text format so it show things like real minutes and seconds? Thanks in advance! Btw someone said to me that i would need to use math to change the mins and secs variables, but i don't know how to do it and in what it would help actually =\

Comment
Bunny83
Edisyo

People who like this

2 Show 1
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 Dakwamine · Nov 28, 2016 at 08:58 PM 0
Share

There is an answer on StackOverflow using the .NET TimeSpan class: What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time

You could also do some maths by dividing the seconds by 60 to get the total time in minutes, get the decimals (the figures after the decimal point) from it to get the seconds by multiplying again by 60, get the time in hours by dividing the minutes (without the figures after the decimal point) by 60, get the decimals from it to get the minutes by multiplying by 60, etc...

This question is not related to Unity, but to general C# scripting, though. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Nov 28, 2016 at 08:48 PM

Just do this:

 int min = Mathf.FloorToInt(timeLeft / 60);
 int sec = Mathf.FloorToInt(timeLeft % 60);
 text.GetComponent<UnityEngine.UI.Text>().text = min.ToString("00") + ":" + sec.ToString("00");

FloorToInt will round the given value down to the nearest integer. So 14.3215 becomes 14.

Dividing your whole time in seconds by 60 gives you minutes but with fractional part so rounding down to int gives you whole minutes

Taking the modulo 60 (division remainder) of the whole time in seconds will give you only the actual seconds Example 885 --> 885 / 60 --> 14 remainder 45. So the time is 14 minutes and 45 seconds.

Comment
Dakwamine
horoxix
mckeithen
polygonalgirl
RenanRL
OnYtoln147
nerasau

People who like this

7 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 Bryangs · Nov 28, 2016 at 09:15 PM 0
Share

Oh, i got it. Thanks! It's working perfectly now =)

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.

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

57 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

Related Questions

Turn seconds into minutes and seconds MM:SS 1 Answer

Javascript beginner here: how do I implement time such as hours and days? 2 Answers

making a timer (00:00) minutes and seconds 10 Answers

Clock Script From Java to C# Help 1 Answer

How Would I Change This To Read Within A Time Range? 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