Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ethranes · Aug 02, 2016 at 11:30 PM · c#timerstringtimer countdowntostring

Remove unnecessary text from a string

Hi,

I have an issue where I have a timer that countsdown between scenes, I'm using a date time to make sure that the timer holds the timer remaining between the scenes.

The problem that I have is that I have a 10 second timer but in the UI it displays as 00:00:10 and I'll never go above a 2 digit number, so I only need the last 2 numbers to be visible, is there any way that I can achieve this with the current code that I have in place?

Here is the code for the countdown timer;

 public static class GlobalCountDown
 {
     static DateTime TimeStarted;
     static TimeSpan TotalTime;
 
     public static void StartCountDown(TimeSpan totalTime)
     {
     

 TimeStarted = DateTime.UtcNow;
     TotalTime = totalTime;
 }

 public static TimeSpan TimeLeft
 {
     get
     {
         var result = TotalTime - (DateTime.UtcNow - TimeStarted);
         if (result.TotalSeconds <= 0)
             return TimeSpan.Zero;            
              return result;
     }
 }

Here is where I initialize the countdown in the scene;

 GlobalCountDown.StartCountDown (TimeSpan.FromSeconds (8))

And this is where I convert the timer into a string value that I'm able to display on the UI;

 countdownTimer.text = GlobalCountDown.TimeLeft.ToString();

I think the change needs to occur where I set the timer ToString but am unsure on how to do this.

I'd appreciate any help or advice with this issue, thanks a lot!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by seth_slax · Aug 02, 2016 at 11:52 PM

Well, this isn't a very efficient way, but you could use Substring.

 string temp = GlobalCountDown.TimeLeft.ToString();
 temp = temp.Substring(temp.Length - 2, 2);
 countdownTimer.text = temp;

There's probably a better way to do this, but this is just off the top of my head. In case you haven't used it before, Substring takes the string and creates a new string out of what you specify (hence not being efficient). The first parameter is how many characters in you want to start (0 being from the first), and the second being how long you want the result to be.

A string is simply an array of type Char. You can even access specific letters using normal array functionality, like myString[index], so perhaps there's something you could do with that instead.

Comment
Add comment · Show 2 · 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 seth_slax · Aug 02, 2016 at 11:59 PM 0
Share

Actually, now that I think of it, you could just modify your TimeLeft method to return an int:

     public static int TimeLeft
      {
          get
          {
              var result = TotalTime - (DateTime.UtcNow - TimeStarted);
              if (result.TotalSeconds <= 0)
                  return 0;            
                   return result.Seconds;
          }
      }

Or even extract that from your original text assignment:

 countdownTimer.text = GlobalCountDown.TimeLeft.Seconds.ToString();

Also as a side note, using 'var' is fine if you're not sure what data type you're assigning, but it's generally a little more efficient to explicitly declare the type, which in this case would be TimeSpan.

avatar image ethranes · Aug 03, 2016 at 05:40 PM 0
Share

Thanks for helping out with this @seth_slax I took your suggestion from your comment;

 countdownTimer.text = GlobalCountDown.TimeLeft.Seconds.ToString();

And it works perfectly, I also switched my var to the correct TimeSpan type.

Cheers!

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

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

Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer

Countdown timer... 2 Answers

Stopwatch will not show time. 1 Answer

Traffic Light won't change colors State machine 0 Answers

How do I decrease a slider over time? 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