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!

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.