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 CaptainIcy281 · Apr 11, 2012 at 05:51 AM · playerprefstimerhighscores

"Best Time"/high score help…

Hi again. Yet another question from me. (I'm learning!) I've been doing a lot of searching regarding this but have yet to find anything useful and as you probably know, I'm not a genius when it comes to scripting, so I'm asking here.

I'm making a game. (my Skyroads-like game I keep mentioning in my questions) The next thing I really need to do to make way for other things, is set up a best time/high score system. Throughout each stage there will be a timer running from the beginning to the end. It will be counting up of course. I want it to get the time at the end, check to see if it's the "best time" and save it. (I guess in PlayerPrefs) I want each individual stage/level to have its own best time of course. Then, at the level select screen, there will be a "Best Time" below each level and it will display the "best time" for that level/stage.

I already have a timer counting up. I haven't told it when to stop yet, since I don't actually have a complete level that "starts" and "ends" but I'll get to that.

I'd just like a place to start. You don't need to write any code out for me if you don't want to. I just want some direction. I've looked into PlayerPrefs a bit, looked at other high score questions, but they're all about a high score table with a name and all that. I just want ONE "high score" (best time taken from the Timer's variable at the end of the level I guess) to be saved and displayed on the level select screen at a certain point on the screen. (Of course) Plenty of other things I might do with it, but I can figure all that out myself. For now I just need some help figuring this out.

……

TL;DR, I want to do this:

  1. Have a timer counting up (on any given stage, whatever, this is already done, unless I need to re-do it to make up for the method I'll use to save it and all)

  2. Save the timer's "time" when a certain thing happens (the level is "completed", I guess a boolean or something, whatever, not important)

  3. Check to see if it's the best time (check the previous best time, if there is none, then it should be 0 or something and it will know there is no best time, so it is automatically the best time)

  4. Display the best time somewhere. I'm not asking for you to write it all out for me, just pointing me in the right direction with a few tips will do.

Sorry for such a long post, I'm somewhat of a perfectionist. Thanks for any help in advance. :)

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 by0log1c · Apr 11, 2012 at 05:58 AM 1
Share

You are so close to doing it all by yourself its not even funny ;)

avatar image Dunkhan · Apr 11, 2012 at 06:03 AM 0
Share

He's right you basically have it. One suggestion though is to use math.infinity ins$$anonymous$$d of 0 on your best time check, that way you only need to do the < test and not an extra test for 0 first.

avatar image Kleptomaniac · Apr 11, 2012 at 06:13 AM 0
Share

You can use $$anonymous$$athf.$$anonymous$$in to evaluate all your scores and return the $$anonymous$$imum. :P

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by by0log1c · Apr 11, 2012 at 06:04 AM

Well this is UnityANSWERs, so I'll take a minute to write a pseudo-code, assuming you use JS. I won't comment it as its pretty self-explanatory.

 static function UpdateScore(currentScore:int)
 {
     var highestScore:int = PlayerPrefs.GetInt(Application.loadedLevelName+"_highscore");
     if(currentScore <= highestScore)
         return;
     
     PlayerPrefs.SetInt(Application.loadedLevelName+"_highscore",currentScore);
     Debug.Log("Congratulation! You just beaten the previous high score of: "+highestScore+" with a score of: "+currentScore+" in the level: "+Application.loadedLevelName+"!!!");
 }
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 CaptainIcy281 · Apr 11, 2012 at 06:35 AM 0
Share

This looks a lot like what I was expecting, and what I need, but it just doesn't really make sense to me. I see this type of stuff in a lot of PlayerPrefs examples.

$$anonymous$$ind adding maybe a few comments (in the script) or just a little clarification on what exactly each thing does? Thanks so much for taking the time, though. :) And I figured I was really close, heh. I'll get this. :D

avatar image by0log1c · Apr 11, 2012 at 07:00 AM 0
Share

Well, basically... The first line of the function retrieves the highest score saved as [NameOfTheCurrentScene]_highscore. The second statement compares both scores, if the previous one was better, the function exits(does not complete). The third statement update the saved high score under [NameOfTheCurrentScene]_highscore. The last line just prints a note to the console for information purpose.

avatar image CaptainIcy281 · Apr 13, 2012 at 03:58 AM 0
Share

Thanks so much! I've got it working. But I am having trouble displaying the "highestScore" (best time) later, such as within that level and especially in another scene (level select screen)

Any help on that using your script? I've seen other methods but they aren't working.

EDIT: Never $$anonymous$$d all that, I've got this script working almost perfectly. Converted it to a time format and made it display where I needed it to, tweaked it a little, etc. And you are right about me almost having it before. Very simple once you know how it all works.

Thanks again!

avatar image yisustornatore · Apr 11, 2014 at 04:25 PM 0
Share

can you put that code pliz

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer highscore trouble 1 Answer

using player time as highscore 1 Answer

One PlayerPref not saving 1 Answer

HighScore - Timer 1 Answer

Is there a decent tutorial for a local high score table (android)? 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