0
\$\begingroup\$

I'm making a game that has a timer showing how long the player has survived from being crushed by pessimism. (Long story) I'm trying to use TimeSpan in Unity to accomplish this but it seems that I'm having trouble finding a way to add time consistently. My output is always 00:00:01. Here is the code I've been working on. Any help would be appreciated.

using UnityEngine;
using System.Collections;
using System;

public class TestScript : MonoBehaviour {

static float timer;
TimeSpan t = TimeSpan.FromSeconds(timer);
TimeSpan add = TimeSpan.FromSeconds(1);

// Update is called once per frame
void Update () {

    TimeSpan span3 = t.Add(add);

    Debug.Log(span3);
}
}

UPDATE

Thanks for your help Jason, here's the code I made in case anybody else wants to know how to do this. The only beef I have is that the milliseconds show 3 characters instead of two, but I'm personally very satisfied with it. Plus, I'm sure that it is something that could be easily fixed.

using System.Collections;
using System;

public class TestScript : MonoBehaviour {



static float timer;

// Update is called once per frame
void Update () {
    timer += Time.deltaTime;
    TimeSpan timeSpan = TimeSpan.FromSeconds(timer);
    string output = String.Format("{0}:{1}:{2}",timeSpan.Minutes,timeSpan.Seconds, timeSpan.Milliseconds);
    Debug.Log(output);
}
}
\$\endgroup\$
4
  • \$\begingroup\$ Try adding t = TimeSpan.FromSeconds(timer); immediately before the assignment to span3 above. Does that resolve your problem? \$\endgroup\$
    – user41442
    Commented Jul 28, 2014 at 3:25
  • \$\begingroup\$ Unfortunately, it didn't work. Thank you for trying though Jason. :D \$\endgroup\$ Commented Jul 28, 2014 at 3:32
  • 1
    \$\begingroup\$ You should probably avoid TimeSpan except to create a string like "00:03:43". Instead, just use your float timer to represent the number of seconds the player has been alive. Then every frame, just add the delta time for that frame to the timer. If you need a string, create a temporary TimeSpan just for the conversion. \$\endgroup\$
    – user41442
    Commented Jul 28, 2014 at 3:39
  • \$\begingroup\$ Thanks Jason, your suggestion is working perfectly. :D I'll edit my answer to include a reference of what I did. \$\endgroup\$ Commented Jul 28, 2014 at 3:55

1 Answer 1

2
\$\begingroup\$

My output is always 00:00:01.

Look at how you initialize your values, though:

  • timer will default to zero
  • t will initialize from timer, and is therefore also zero
  • add will default to one second

So we have zero, zero, and one.

In each frame, you create a new TimeSpan value by adding t and add. It's important to note that calling Add does not modify either input. Zero plus one is always one, so your new timespan is always one second.

If you wanted the time to accumulate, you would need to keep the value of that Add operation:

t = t.Add(add);

Althooough, adding one second per frame will probably cause unexpected results. ;)

As mentioned in comments, most Unity games do this by counting seconds outright, usually using some mix of Time.deltaTime, Time.time, or Time.realtimeSinceStartup.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .