- Home /
Problem calculating time taken to complete level
Hi Guys,
I'm a Unity newbie needing help with my script. I have a countdown timer while the level is being played as well as a stopwatch timer calculating how long it took for the level to be completed on the result screen. The issue I have is the stopwatch is not calculating the time correctly and I'm not sure why it is wrong. Any help is appreciated.
Here is my script:
public class Timer : MonoBehaviour {
public static float curSeconds = 59;
public static float curMinutes = 4;
public static float startSeconds = 0;
public static float startMinutes = 0;
public static float startTime = 0;
public static float finishTime;
void Start () {
GameObject.Find ("TimerText").guiText.text = "Time Remaining: " + curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
guiText.material.color = Color.black;
}
//Stopwatch Time
void OnLevelWasLoaded(int level) {
if (level == 5)
finishTime = (curMinutes + curSeconds) - (startMinutes + startSeconds);
GameObject.Find ("TimeSpentText").guiText.text = " " + finishTime.ToString();
guiText.material.color = Color.black;
}
// Countdown Timer
void Update () {
startTime = startMinutes + startSeconds;
startTime = Time.time;
if(curSeconds <= 0) {
curSeconds = 59;
if(curMinutes >= 1)
{
curMinutes--;
}
else {
curMinutes = 0;
curSeconds = 0;
// This makes the guiText show the time as X:XX. ToString.("f0) formats it so there is no decimal place.
GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
}
}
else {
curSeconds -= Time.deltaTime;
}
//These lines will make sure the time is shown as X:XX and not X:XX.XXXXXX
if(Mathf.Round(curSeconds) <= 9) {
GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
}
else {
GameObject.Find ("TimerText").guiText.text = curMinutes.ToString("f0") + ":" + curSeconds.ToString("f0");
}
}
}
Answer by paulaceccon · Aug 09, 2013 at 12:13 AM
float currentTime;
float startTime;
float roundedRestSeconds;
float displaySeconds;
float displayMinutes;
void Start () {
startTime = Time.time;
}
void OnLevelWasLoaded(int level) {
if (level == 5)
string text = string.Format ("{0:00}:{1:00}", Mathf.CeilToInt(currentTime) % 60;, Mathf.CeilToInt(currentTime) / 60);
//Set this text to your GUI object
}
void Update () {
currentTime = Time.time - startTime;
restSeconds = countDownSeconds - (currentTime);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
string text = string.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
//Set this text to your GUI object
}
When I try and replace my script with yours, I get this error message:
Assets/Scripts/Timer.cs(18,124): error CS1023: An embedded statement may not be a declaration or labeled statement
It's referring to this line:
string text = string.Format ("{0:00}:{1:00}", $$anonymous$$athf.CeilToInt(currentTime) % 60, $$anonymous$$athf.CeilToInt(currentTime) / 60);
I tried declaring the string separately, but it still doesn't work. Am I missing something?
Thank you for all your assistance.
But I still seem to be experiencing an issue with showing the time it takes to complete the level. It appears to be a NullReferenceException for my GameObjects, but I verified and both my GameObjects are correctly named. SO I'm not sure how to fix this.
I am not good at scripting, so thank you for bearing with me. This is what my script looks like now.
public static float curSeconds = 59;
public static float cur$$anonymous$$inutes = 4;
public static float startSeconds = 0;
public static float start$$anonymous$$inutes = 0;
public static float startTime;
public static float finishTime;
public static float currentTime;
public static float roundedRestSeconds;
public static float displaySeconds;
public static float display$$anonymous$$inutes;
public static float countDownSeconds;
public static float restSeconds;
void Start () {
GameObject.Find ("TimerText").guiText.text = "Time Remaining: " + cur$$anonymous$$inutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
guiText.material.color = Color.black;
startTime = Time.time;
}
void OnLevelWasLoaded(int level) {
if (level == 5) {
string text = string.Format ("{0:00}:{1:00}", $$anonymous$$athf.CeilToInt(currentTime) % 60, $$anonymous$$athf.CeilToInt(currentTime) / 60);
GameObject.Find ("TimeSpentText").guiText.text = " " + text.ToString();
guiText.material.color = Color.black;
}
}
void Update () {
currentTime = Time.time - startTime;
restSeconds = countDownSeconds - (currentTime);
roundedRestSeconds = $$anonymous$$athf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
display$$anonymous$$inutes = roundedRestSeconds / 60;
string text = string.Format ("{0:00}:{1:00}", display$$anonymous$$inutes, displaySeconds);
GameObject.Find ("TimerText").guiText.text = " " + text.ToString();
guiText.material.color = Color.black;
startTime = start$$anonymous$$inutes + startSeconds;
startTime = Time.time;
if(curSeconds <= 0) {
curSeconds = 59;
if(cur$$anonymous$$inutes >= 1)
{
cur$$anonymous$$inutes--;
}
else {
cur$$anonymous$$inutes = 0;
curSeconds = 0;
// This makes the guiText show the time as X:XX. ToString.("f0) formats it so there is no decimal place.
GameObject.Find ("TimerText").guiText.text = cur$$anonymous$$inutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
}
}
else {
curSeconds -= Time.deltaTime;
}
//These lines will make sure the time is shown as X:XX and not X:XX.XXXXXX
if($$anonymous$$athf.Round(curSeconds) <= 9) {
GameObject.Find ("TimerText").guiText.text = cur$$anonymous$$inutes.ToString("f0") + ":0" + curSeconds.ToString("f0");
}
else {
GameObject.Find ("TimerText").guiText.text = cur$$anonymous$$inutes.ToString("f0") + ":" + curSeconds.ToString("f0");
}
}
}
It's not usually a good idea to use GameObject.Find. I recommend putting GameObject timerText and GameObject timeSpentText as public variables with your public floats and what not. You can then directly link those objects in the Unity inspector and not have to worry about them potentially turning up null or from a typo in the object name.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111807im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Time difference between race time and best time 1 Answer
Multiple Cars not working 1 Answer
How to realize accurate time counter (millisecond precision) 3 Answers
Start timer on mouse0 click 1 Answer