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 trevmate · Aug 31, 2015 at 12:23 PM · guitimercountdown

converting timer countdown to only show minute and seconds

So I'm using C# and have a count down timer, if the timer hits 0 then the player dies. i have a timer working but its showing seconds and milliseconds only (e.g. 6.5377) i need to change it to only show minute and seconds. the time shows up on my GUI just fine (other than showing seconds/milliseconds) This is the code for the timer

 public float timeRemaining = 120;
     void Update () 
     {
         if (timeRemaining <=0)
         {
             timeRemaining =0;
         }
         timeRemaining-=Time.deltaTime;
     }
 and this is the code for the GUI to show
 public Text timeRemaining;
     public GameObject countDownRef;
 void Update()
 {
 timeRemaining.text= countDownRef.GetComponent().timeRemaining.ToString();
 }

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 Scribe · Aug 31, 2015 at 12:33 PM 0
Share

please format your code. Select it all and press the '1010101' button or ctrl+$$anonymous$$

avatar image trevmate · Sep 01, 2015 at 08:03 AM 1
Share

sorry didnt know how to format it, thanks Scribe for letting me know

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bob-The-Zealot · Aug 31, 2015 at 12:45 PM

What your looking for is the math operation mod.
The mod symbol is %.
You would do: seconds = totalseconds % 60; to calculate the seconds,
and minutes = Mathf.Round(totalseconds / 60) to calculate the minutes;

So you would do this:

timeremaining.text = Mathf.Round(totalseconds/60) + ":" + totalseconds % 60;

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 trevmate · Sep 02, 2015 at 06:33 AM 0
Share

how do I implement this into my code? I'm still rather new to coding

avatar image Bob-The-Zealot · Sep 02, 2015 at 10:14 AM 0
Share

@trevmate I edited my answer.

avatar image trevmate · Sep 02, 2015 at 12:37 PM 0
Share

@Bob the Zealot i got it showing the $$anonymous$$ute and second, but it still shows milliseconds also the time isnt count correctly, e.g. if totalSeconds is set to 180 it starts at 4:00 but then when time gets down to 3:30 it then becomes 2:29 when it hits 2:00 then it becomes 2:59 then at 2:30 it becomes 1:29 its not until the last 30seconds that the timer has the correct time is this how you meant for it to be set up on the canvas script

public Text timeRemaining; public float totalSeconds = 100; timeRemaining.text = $$anonymous$$athf.Round(totalSeconds/60) + ":" + totalSeconds % 60; totalSeconds-=Time.deltaTime;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Countdown timer for starting game 0 Answers

Problems showing accurate time with FixedUpdate() 0 Answers

FPS Styled Timer Game: Script for starting a timer on play and stopping the timer when win condition achieved. 1 Answer

Start and Stop Timer Help 1 Answer

DateTime with countdown 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