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 mradebe_eti · Oct 18, 2013 at 12:37 AM · c#playerprefssavescore

Problem with High Score System using PlayerPrefs [C#]

Hi,

I'm trying my first PlayerPrefs high score system. I need to save the best score and time so I can have them display on the results screen and the high score screen. I'm using the solution from the example below as my base script: http://answers.unity3d.com/questions/20773/how-do-i-make-a-highscores-board.html

This is what I currently have:

 using UnityEngine;
 using System.Collections;
 
 public class HighScoreS1 : MonoBehaviour {
     
         int newScore;
         int newTime;
         int oldScore;
         int oldTime;
     
     void AddScore(int score, int time){
         newScore = score;
         newTime = time;
         
         for(i = 0; i < 10; i++){
             if(PlayerPrefs.HasKey(i + "HScore")){
                 if(PlayerPrefs.GetInt(i + "HScore") < newScore){
                     //new score is higher than the stored score
                     oldScore = PlayerPrefs.GetInt(i + "HScore");
                     oldTime = PlayerPrefs.GetInt(i + "HScoreTime");
                     PlayerPrefs.SetInt(i + "HScore",newScore);
                     PlayerPrefs.SetInt(i + "HScoreTime",newTime);
                     newScore = oldScore;
                     newTime = oldTime;
                 }
             } else{
                 PlayerPrefs.SetInt(i + "HScore",newScore);
                 PlayerPrefs.SetInt(i + "HScoreTime",newTime);
                 newScore = 0;
                 newTime = 0;
             }
         }
     }
 }

Any help is appreciated. Thanks.

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 robertbu · Oct 18, 2013 at 12:45 AM 0
Share

I don't see a question here. What is it not doing?

avatar image mradebe_eti · Oct 18, 2013 at 12:53 AM 0
Share

The script is not compiling, but I've copied as much as I can from the above solution. Is there something I'm doing wrong?

avatar image Benproductions1 · Oct 18, 2013 at 01:01 AM 0
Share

"It doesn't compile", well how about you post the compile error then? Or even try and read/understand it yourself!

1 Reply

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

Answer by mattmanj17 · Oct 18, 2013 at 02:00 AM

On line 15, when you begin your for loop, you forgot to write "`int i=0`" instead of just "`i=0`". With out the "`int`", i is never initialized, and the references to "`i`" in the for loop will not make sense. this leads to the error...

 error CS0103: The name `i' does not exist in the current context

Just adding "`int`" in the for loop should fix it. The rest of the code seems fine as far as I can tell. Hope this helps, and good luck with your project!

Comment
Add comment · Show 3 · 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 mradebe_eti · Oct 18, 2013 at 05:58 PM 0
Share

Thanks mattmanj17! You've been a great help.

avatar image mattmanj17 · Oct 18, 2013 at 06:50 PM 0
Share

glad I could help! if you like my answer, select it as the "right" answer, so your question can be marked as resolved.

avatar image A-Tunga · Aug 17, 2014 at 02:41 PM 0
Share

Thanks that Works...

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

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

Save player position when trigger is destroyed 1 Answer

Can anyone help me figure out why PlayerPrefs wont save the money value?,Can anyone help me as to why my money won't save in my game? Using PlayerPrefs 1 Answer

How to make a Saved/Favorites system in unity? 0 Answers

Highscores PlayerPrefs Sorting Issue 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