Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 /
This question was closed Jun 16, 2014 at 07:30 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by m3xr3al · Jun 16, 2014 at 03:12 PM · c#timetimer

Game Timer help needed

Attempting to make a script that runs a timer that keeps track of how long you survived in game, and also updates the highscore if the timer is larger than it. This all works perfectly right now and i have achieved the above, but now I've been trying to have the game and timer reset when you lose, the only problem is that the timer just pauses and then upon reset carries on from where it left off. I need to set the value of the timer to zero but this doesn't seem to work, any help??

For the GUI to display the Timer

 public class Timer : MonoBehaviour {
  
 //Reference for timer layout
 public int minutes;
 public int seconds;
 public int fraction;    
 
 //Timer
 public static float countTime;
 
 //bool "go" is available to use by other scripts also
 public static bool go;
  
     void Start(){
     
     go = true;
     
     }
  
     void Update () 
     {
 
     //Only happen if go = true 
     //Triggers and displays the time with correct format upon go = true
     if(go) {
 
       countTime = Time.time ;
       minutes = (int)(countTime/60f );
       seconds = (int)(countTime % 60f);
       fraction =  (int)((countTime *10) %10);
       guiText.text = string.Format("{0}   .   {1}   .   {2}", minutes, seconds, fraction);
       }
       
     }
 }



For a seperate GUI to display a highscore as explained above

public class Bestscore : MonoBehaviour {

//For best time public int minutesB; public int secondsB; public int fractionB;

//Defines a public float "bestTime" public float bestTime = 0f;

 void Start() {
 
 //On startup PlayerPrefs are loaded
 bestTime = PlayerPrefs.GetFloat("bestTime", 0f);
 
 } 

 //At the end of the game run below
 void OnDestroy() {
 
 //Saves the data for later use
 PlayerPrefs.SetFloat("bestTime", bestTime);
 }
 
 void Update() {
  
 //If score is higher than the high score the timer will match the score
 if(Timer.countTime > bestTime) {
   bestTime = Timer.countTime;

   }
 }
   
 void FixedUpdate() {
 
 //Layout for high score GUI text 
 minutesB = (int)(bestTime/60f );
 secondsB = (int)(bestTime % 60f);
 fractionB =  (int)((bestTime *10) %10);
 guiText.text = string.Format("BEST TIME    :    {0}   .   {1}   .   {2}", minutesB, secondsB, fractionB);       
 }

}

I've tried adding countTime = 0f; to the start but that dosen't do anything. Sorry if this is too long or troublesome, any help would be appreciated and i'll be on in the evening to check back in.

Comment
Add comment · Show 1
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 meat5000 ♦ · Jun 16, 2014 at 04:16 PM 0
Share

This is a SERIOUSLY well documented question.

It's so duplicated that its like The Borg. There is absolutely nothing about timers that you can't find in a thousand different existing QA's on Unity Answers. Please search and do your research before posting!

No offense intended.

The only reason this question is not closed is that 3 people have taken the time to answer. I won't do them the mis-justice.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Phles · Jun 16, 2014 at 03:52 PM

Hey,

Instead of using Time.time increment countTime by Time.deltaTime;

 countTime += Time.deltaTime;

You can then simply do countTime = 0; to rest the timer, Time.time is the seconds since the game started you don't want to use that here.

Phill

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 m3xr3al · Jun 16, 2014 at 07:29 PM 0
Share

This worked perfectly, thanks so much for suggesting it, whether or not the question seemed stupid i really appreciate the help!

avatar image meat5000 ♦ · Jun 16, 2014 at 07:34 PM 0
Share

Could you try Unaccepting and reaccepting the answer? Seems to have bugged out in the list.

avatar image
0

Answer by Minchuilla · Jun 16, 2014 at 03:42 PM

What I would do is this:

  1. use a variable to decide when the timer has been started

ie.

 //when button is clicked or whatever set timerStarted to true
 if(timerStarted)
 {
    timeOfStart = Time.time;
    timerStarted = false;
 }
 
 if(IAmTiming)
 {
    float currentTime = Time.time - timeOfStart;
 
    minuites = (int)(currentTime/60);
    //etc
 }

Then each time you want to reset the timer set timerStarted to true

Feel free to ask for more detail or prove me wrong.

Minchuilla

Comment
Add comment · Show 1 · 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 m3xr3al · Jun 16, 2014 at 07:22 PM -1
Share

I'll try out some of these and see if i can get it working, thanks for the feedback. I'll let you guys know how it goes.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to work a real life timer? 2 Answers

How to make a Time Based Score? 1 Answer

Timer doesn't work properly 2 Answers

How to make a timer read to the .001 of a second 2 Answers

Multiple Cars not working 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