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
1
Question by Sean Scofield · Jan 03, 2011 at 09:35 PM · playerprefssavescorehighscores

high scores scene

I'm working on a game in which the score is the amount of time the player is alive. So far I am using PlayerPrefs to save the score with this script: EDIT:

private var alive = true; function OnCollisionEnter(collision : Collision) {

if(collision.gameObject.tag == "blocks") { Time.timeScale = 0.000001; audio.Pause (); Invoke("thescore",0); alive = false; } } function thescore () { var score : float = Time.timeSinceLevelLoad;

if( score > PlayerPrefs.GetFloat("Score1") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat( "Score2", PlayerPrefs.GetFloat("Score1") ); PlayerPrefs.SetFloat("Score1", score); } else if( score > PlayerPrefs.GetFloat("Score2") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat("Score2", score); } else if( score > PlayerPrefs.GetFloat("Score3") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat("Score3", score); } else if( score > PlayerPrefs.GetFloat("Score4") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat("Score4", score); } else if( score > PlayerPrefs.GetFloat("Score5") ){ PlayerPrefs.SetFloat("Score5", score); } }

Here's an example of the new High score script that is attached to one of my guitexts in the high score scene: function Start () { guiText.text = (PlayerPrefs.GetFloat("score1")).ToString (); }

My new problem is that the high scores won't update, but instead stay at zero all the time.

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 PrimeDerektive · Jan 04, 2011 at 01:05 AM 0
Share

It looks like all the PlayerPrefs you are setting in your previous scene have a capital "S" in score, but your sample code from your high score scene is trying to pull PlayerPrefs with a lowercase "s", is that it?

avatar image Sean Scofield · Jan 04, 2011 at 01:37 AM 0
Share

Success! Thanks a lot for your help. Would never have figured it out on my own.

avatar image PrimeDerektive · Jan 04, 2011 at 02:35 AM 0
Share

No problem! Glad to help; I thought it was an interesting challenge that I might have use sometime.

2 Replies

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

Answer by PrimeDerektive · Jan 03, 2011 at 09:49 PM

Couple things:

Your going to need 5 PlayerPrefs, for the top 5 scores. I'll call them score1, score2, score3, and so on.

Second, you'll want to store them in PlayerPrefs as floats, instead of strings, and just do the .ToString() for you GUI after you retrieve them for the high score scene (that way, you can actually do math comparisons with them).

Then you'll probably want to do something like this (EDIT: after Ej's comment I realized you have to shift all the scores down one, from the bottom up, if you are overwriting a score [unless its score 5, because theres no score 6 to shift it to, so it just gets dumped] ):

var score : float = Time.timeSinceLevelLoad;

if( score > PlayerPrefs.GetFloat("Score1") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat( "Score2", PlayerPrefs.GetFloat("Score1") ); PlayerPrefs.SetFloat("Score1", score); } else if( score > PlayerPrefs.GetFloat("Score2") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat("Score2", score); } else if( score > PlayerPrefs.GetFloat("Score3") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat("Score3", score); } else if( score > PlayerPrefs.GetFloat("Score4") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat("Score4", score); } else if( score > PlayerPrefs.GetFloat("Score5") ){ PlayerPrefs.SetFloat("Score5", score); }

Comment
Add comment · Show 6 · 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 Ejlersen · Jan 03, 2011 at 09:59 PM 2
Share

Wouldn't that just overwrite one of the scores? $$anonymous$$g. I get a score greater than score2, so I overwrite that score, but the previous score2 was greater than score3, score4, and score5, but now we have lost that information?

avatar image PrimeDerektive · Jan 03, 2011 at 10:41 PM 0
Share

Holy crap! $$anonymous$$assive oversight on my part... Thanks Ej. Let me think about this for a second...

avatar image PrimeDerektive · Jan 03, 2011 at 11:17 PM 0
Share

Does that look right?

avatar image Sean Scofield · Jan 03, 2011 at 11:26 PM 0
Share

Ok I've read the script you've provided and it makes sense to me. I'll try it out and let you know what happens. Thanks for the info.

avatar image Sean Scofield · Jan 04, 2011 at 12:25 AM 0
Share

$$anonymous$$. I tried what you said and it's not quite working. There are no compiler errors. $$anonymous$$y highscore scene has 5 gui texts in it each with a different script attached to it, an example being: function Start () { guiText.text = (PlayerPrefs.GetFloat("score1")).ToString (); } However, the scores are always the default of zero and never change. Each score is simply zero and I don't know where to go from here.

Show more comments
avatar image
0

Answer by Jackolanten · Apr 04, 2013 at 12:28 AM

hi i already do that script above but when my player finish all stage all score from 1 to 5 is same as high score, but when i game over the socre is normal.. i dunno whats wrong with the script

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

1 Person is following this question.

avatar image

Related Questions

PlayerPrefs HighScore problems, it doesn't work. 4 Answers

Recognising high score when updated with a graphic 0 Answers

Highscoring and changing scripts (Java/UnityScript) 1 Answer

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

Playerprefs When the value I set is zero, there is a 2x increase in the part I get. when i close and reopen the game it only shows the last value. How can ı solve these? 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