Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by mradebe_eti · Aug 08, 2013 at 07:32 PM · c#timercalculation

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");
     }
 }

}

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
0

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
 }
Comment
Add comment · Show 4 · 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 mradebe_eti · Aug 09, 2013 at 05:49 PM 0
Share

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?

avatar image paulaceccon · Aug 09, 2013 at 06:04 PM 0
Share

http://stackoverflow.com/questions/2496589/variable-declarations-following-if-statements

avatar image mradebe_eti · Aug 09, 2013 at 09:46 PM 0
Share

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");
 }
 }

}

avatar image SethJH · Aug 10, 2013 at 12:17 AM 0
Share

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

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

16 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

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


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