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 josak46 · Jul 18, 2014 at 09:06 PM · timeplayerprefssavebest

Save best time

Hi!

I have a problem in my script. This is my script. The problem is that the best time doesn't save.

 static var currentScore : int = 0;
 var timer : float = 0.00;
 var bestTime : float;
 
 
 function Start () {
  
 }
 function Update () {
  if (currentScore == 20) {
    if (bestTime > timer){
   bestTime = timer;
   var bestTimer = PlayerPrefs.GetFloat("bestTimer");
   PlayerPrefs.SetFloat("bestTimer", bestTimer);
   PlayerPrefs.Save();
   }
  }
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(90, 10, 70, 20), "" + timer.ToString (""));
  
 }

Timer script:

 var timer : float = 0.00;
 static var currentScore : int = 0;
 
 function Update () 
 {
     timer += Time.deltaTime;
    
     
 }
 
 
 
 function OnGUI ()
 
 {
     GUI.Box(new Rect(10, 10, 70, 20), "" + timer.ToString (""));
     
 }


Thank you. :)

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
1

Answer by Lo0NuhtiK · Jul 18, 2014 at 09:33 PM

Idk, man, your stuff is just weird lookin today... er somethin.

Try this instead of having two scripts that don't talk to eachother.

 static var currentScore : int = 0 ;
 private var timer : float = 0f ;
 private var bestTime : float = 0f ;

 funyuns Start()
 {
    bestTime = PlayerPrefs.GetFloat("BestTime", Mathf.Infinity) ;
 }
 
 funyuns Update()
 {
    SetTimers() ;
 }

 funyuns SetTimers()
 {
    timer += Time.deltaTime ;
    
    if(currentScore == 20)
    {
       if(timer < bestTime)
       {
          PlayerPrefs.SetFloat("BestTime", timer) ;
          bestTime = timer ;
       }
    }
 }
 
 
 funyuns OnGUI()
 {
    GUILayout.Box("Timer : " + timer) ;

    GUILayout.Box("Best Time : " + bestTime) ;
 }


Comment
Add comment · Show 7 · 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 josak46 · Jul 19, 2014 at 01:23 PM 0
Share

Timer works great but it writes Best time : bestTime and when I reach score 20 it still writes Best time : bestTime. :/ Do you know why? Thank you for your help.

avatar image Lo0NuhtiK · Jul 19, 2014 at 04:10 PM 0
Share

I had put a couple things in the wrong spot and didn't test it or anything. Just edited the original post, still didn't test it, but now it might work how you want lol ...

avatar image josak46 · Jul 19, 2014 at 07:40 PM 0
Share

Assets/skripte/timer.js(23,16): BCE0051: Operator '<' cannot be used with a left hand side of type 'float' and a right hand side of type 'Object'.

I tried to do private var bestTime : float; but it doesn't work as I should. :)

And in the game it writes Best time : Infinity.

avatar image Lo0NuhtiK · Jul 20, 2014 at 03:36 AM 0
Share

I had to go check on my other PC a few $$anonymous$$utes ago after seeing your comment so I could make a js script and see wtf lol...

I don't use js, and this would've worked fine in c# had I written it in that xD ... I don't remember UnityScript being quite so strict when I first used a few years ago, so idk. [var whatever;] usually suffices for a js post I make lol

I tried it with just using a few big numbers ins$$anonymous$$d of infinity, nope that didn't work either.

Its the variable declaration like you thought.

 var timer : float ; //var timer = 0f ; worked also, I checked
 var bestTime : float ;


And that should do it.

Yeah, the GUI will show Infinity until you get the first "BestTime" set, (if you don't want it to say infinity, make it say something else ins$$anonymous$$d), and also this needs changed->

 if(timer < bestTime)
     PlayerPrefs...whatever...

...to->

 if(timer < bestTime)
 {
    PlayerPrefs.SetFloat("BestTime", timer) ;
    //and then set bestTime to match so it wont keep updating
    //and so your gui will show it
    bestTime = timer ;
    //but I figured you would have been adding stuff to change levels or do something else at this point 
    //since you had reached a score to set a best time for...
 }


Anyway, pretty sure all I did to make it work was changed the variable declarations to specify : float ... if not, I guess I'll go back upstairs and check that test js script again xD

Edit : (Edited original script post, now "maybe" it'll do wtf its supposed to lol)

avatar image josak46 · Jul 20, 2014 at 01:40 PM 0
Share

It doesn't :(. I'm gonna' cry :). I don't know... I tried to change something with my noob knowledge but it doesn't save and bestTime is always Infinity. It doesn't change. I'd try to help you but i don't know.

Thank you anyway.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

playerprefs problem 0 Answers

Playerprefs save player position 3 Answers

How can i get the following script back on track to being a single level high score saver 0 Answers

PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers

Playerprefs to save unlocked button 2 Answers


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