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 cah_98 · May 12, 2013 at 06:41 PM · javascripttimer

JavaScript Timer/Highscore Problem

I tried to make a script which would let me press a button to see the current time that has passed since the level started but it just prints 0 when i press the button. I have pressed I to stop the recording and tried in many different ways. Im a bit new to java so please be patient with me. Thanks for any help i might get.

here is my codes:

 static var record : boolean;
 static var currentTime : float;
 static var bestTime : float;
  
 function Start () {
     // Reset current time
     currentTime = 0.0;
  
     // Start record
     StartRecord();
 }
  
 function Update () {
     // Keep adding to currentTime if record is true
     if (record)
         currentTime += 1*Time.deltaTime;
  
     // If Player press R then run Respawn function
     if (Input.GetKeyDown(KeyCode.R))
         Respawn();
         
     // If Player press I then stop recording time
     if (Input.GetKeyDown(KeyCode.I))
         StopRecord(true);
 }
  
 function Respawn () {
     if (!Application.isLoadingLevel)
         Application.LoadLevel(Application.loadedLevel);
 }
  
 static function StartRecord () {
     record = true;
 }
 
 static function StopRecord (checkBestTime : boolean) {
     record = false;
     if (checkBestTime && currentTime<bestTime) {
         bestTime = currentTime;
         PlayerPrefs.SetFloat("Score", bestTime);
     }
 }

And this:

 function OnGUI () {
     if (GUI.Button (Rect (0,0,200,100), "Click for highscore")) {
         print (PlayerPrefs.GetFloat("Score"));
     }
 }
Comment
Add comment · Show 3
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 TheDarkVoid · May 12, 2013 at 07:38 PM 0
Share

where is "checkBestTime" defined, what is it's value.

avatar image cah_98 · May 12, 2013 at 08:39 PM 0
Share

Thanks for the help now it works as intended.

avatar image Crazydadz · May 13, 2013 at 12:39 AM 0
Share

Can you please tell us what you did. You asked a question and you found the solution. UA is here to help everyone. You are not helping anyone if you are not giving us your solution.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Crazydadz · May 12, 2013 at 08:09 PM

your default besttime value is 0. So you will never beat your best time. You should first check in your StopRecord function if bestTime is 0 then set your bestTime to your currentTime. After that, this should work.

 static function StopRecord (checkBestTime : boolean) {
     record = false;
     if (checkBestTime && (currentTime<bestTime || bestTime == 0)) {
         bestTime = currentTime;
         PlayerPrefs.SetFloat("Score", bestTime);
     }
 }
Comment
Add comment · 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

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

Elevator script problem (Java) 1 Answer

Timer Between Labels 2 Answers

Need some help an object collider that stops a timer then displays it on another scene 1 Answer

Talking Code 2 Answers

counting seconds that character is idle. 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