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 Ekta-Mehta-D · Apr 12, 2013 at 05:28 AM · javascriptplayerprefshighscores

HighScore Manage Using PlayerPrefs

I want to manage HighScore levelwise..For that I am using PlayerPrefs plugin.

I have written below code to manage highscore:

ScoreController Script in JS:

 public var highScoreList : List.<int> = new List.<int>();

 function Start () {
     LoadData();
     gameControllerScript = GameObject.Find("Main Camera").GetComponent("GameController");
 }

 function LoadData()
 {
     for (var i : int = 0; i < highScoreList.Count; i++)
     {
         highScoreList[i] = PreviewLabs.PlayerPrefs.GetInt("highScore" + i);
     }
 }

 function Update () {
 if (!once)
     {
         if(GameController.Score >= gameControllerScript.objectiveScore)
         {
                 if(GameController.HighScore < GameController.Score)
             {
                 highScoreList[GameController.level] = GameController.Score; // Error Comes From Here
                 Debug.Log("new high score: " + GameController.Score);
                 SaveData();
             }
                     once = true;
         }
       }
 }
 function SaveData()
 {
     for (var i : int = 0; i < highScoreList.Count; i++)
     {
         PreviewLabs.PlayerPrefs.SetInt("highScore" + i,highScoreList[i]);
     }
     PreviewLabs.PlayerPrefs.Flush();
 }

But it gives me ArgumentOutOf range Exception.. So how do i store and update my highscore for perticuler level?

Consol :

 Argument is out of range.
 Parameter name: index
 System.Collections.Generic.List`1[System.Int32].CheckIndex (Int32 index)
 System.Collections.Generic.List`1[System.Int32].set_Item (Int32 index, Int32 value)

Pleaze Help me..

Thanx for your Support and Help..

Comment
Add comment · Show 6
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 · Apr 12, 2013 at 05:37 AM 0
Share

Please copy the error message from your Console and post it here. Also add a comment to the code above on the line that is generating the IndexOutOfRange Exception. Note you have not included the code that initializes 'higheScoreList' above.

avatar image Ekta-Mehta-D · Apr 12, 2013 at 05:42 AM 0
Share

oops.. sorry sir. its argumentoutofrange..

avatar image Eric5h5 · Apr 12, 2013 at 06:26 AM 1
Share

Use ArrayPrefs2 to save arrays using PlayerPrefs.

avatar image Ekta-Mehta-D · Apr 12, 2013 at 06:34 AM 0
Share

@Eric5h5 But What is the problem with this script?? and sir can you plese show me the way of using ArrayPrefs2??

avatar image Eric5h5 · Apr 12, 2013 at 06:39 AM 0
Share

The page I linked to has instructions; read them. I posted that as a comment ins$$anonymous$$d of an answer because if you use ArrayPrefs2, you don't need that script at all.

Show more comments

1 Reply

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

Answer by robertbu · Apr 12, 2013 at 06:04 AM

You are using a list, but you are not adding any elements into the list. So when you index into the list you get an error. If you know ahead of time the number of levels you will have you can use a built-in array with very little restructuring of your code.

 var highScoreList: int[] = new int[100];

Then you will use 'Length' instead of 'Count':

 for (var i : int = 0; i < highScoreList.Length; i++)
Comment
Add comment · Show 5 · 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 Ekta-Mehta-D · Apr 12, 2013 at 06:17 AM 0
Share

Now it throws error At Same Point:

 IndexOutOfRangeException: Array index is out of range.
 ScoreController.Update () (at Assets/Scripts/ScoreController.js:62)
avatar image Ekta-Mehta-D · Apr 12, 2013 at 06:18 AM 0
Share

$$anonymous$$y level variable is having 1 value..

avatar image robertbu · Apr 12, 2013 at 06:38 AM 0
Share

You are saying that GameController.level == 1? You Debug.Log()ed this to verify? And did you initialize highScoreList?

avatar image Ekta-Mehta-D · Apr 12, 2013 at 06:53 AM 0
Share

yes sir.. and yes gamecontroller.level == 1.. i have debugged this to verify..

avatar image Ekta-Mehta-D · Apr 13, 2013 at 04:52 AM 0
Share

Thanx sir.. Your solution works for me..

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

11 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

Related Questions

How would I transfer these into PlayerPrefs? 0 Answers

how to save and retreive high score.... 4 Answers

displaying playerprefs 1 Answer

Highscoring and changing scripts (Java/UnityScript) 1 Answer

High Score with player prefs confusion (JAVA) 0 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