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 AusieJamster · Mar 19, 2014 at 12:02 PM · c#errorarrayshighscoressorting

Scoring System ArgumentOutOfRangeException Error

Ive been trying to do this scoring system for weeks but i still seems to get stuck and i have no idea why :( it comes up with an error of "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SaveLoad
 {
     public static string name = "Enter your Name";
 
     private static List<HighscoreHolder> score = new List<HighscoreHolder>();
 
     public void Start()
     {
         List<HighscoreHolder> score = new List<HighscoreHolder>();
     }
 
     public static void Save ()
     {
         Load(); // Load current highscore list for comparison
         
         if (GameManager.score > score[4].playerScore)// If you get a high score
         {
             name = score[5].playerName; //input player name into temp highscore
 
             score[5].playerScore = GameManager.score;
             
             //Sort array by score
             score.Add(new HighscoreHolder { playerName = name, playerScore = GameManager.score });
             score.Sort((s1,s2) => s2.playerScore == s1.playerScore ? 0 : (s2.playerScore < s1.playerScore ? 1 : -1));
             while(score.Count >= 6)
                 score.RemoveAt(5);
 
             //Put Score array top 5 back into player prefs
             for (int i = 0; i < 5; i++)
             {            
                 PlayerPrefs.SetString("Name" + i, score[i].playerName);
                 PlayerPrefs.SetInt("Score" + i, score[i].playerScore);
             }
         }
     }
     
     private static void Load ()
     {
         SetDefault();
 
         for (int i = 0; i < 5; i++)
         {            
             score[i].playerName = PlayerPrefs.GetString("Name" + i);
             score[i].playerScore = PlayerPrefs.GetInt("Score" + i);
         }
     }
     public static void SetDefault()
     {
         if(SaveLoad.score[0].playerScore == null)
         {
             PlayerPrefs.SetString("Name0", "Josh");
             PlayerPrefs.SetInt("Score0", 1000);
             
             PlayerPrefs.SetString("Name1", "Peter");
             PlayerPrefs.SetInt("Score1", 500);
             
             PlayerPrefs.SetString("Name2", "Sarah");
             PlayerPrefs.SetInt("Score2", 250);
             
             PlayerPrefs.SetString("Name3", "Jamie");
             PlayerPrefs.SetInt("Score3", 100);
             
             PlayerPrefs.SetString("Name4", "Homeless Guy");
             PlayerPrefs.SetInt("Score4", 50);
         }
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public class HighscoreHolder
 {
     public string playerName;
     public int playerScore;
 }
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

2 Replies

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

Answer by ShadoX · Mar 19, 2014 at 01:13 PM

basically that means that you're trying to access a object in a list/array (probably the "List score") which doesn't exist..

An example would be string[] score = {"a","b","c"} you can get all the elements of score via score[0], score[1], score[2].. but if you'd try score[3] or anything above that (or below 0) you would receive an Out of Bounds Exception because there is no such element..

Try checking the error again, or clicking on it. Unity should open the script editor and show you the exact line where you are trying to access the element that doesn't exist.

And then try to figure out why the code is trying to do it and fix it :)

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 AusieJamster · Mar 19, 2014 at 04:18 PM 0
Share

it comes up with line 47 which i think is odd because there is a element in each position. :\ i must not of declared it correctly.

avatar image eightbitstev · Mar 19, 2014 at 04:25 PM 0
Share

It looks like you declare it correctly, but here's a problem I see. At line 45, you loop exactly 5 times. That means if your list ever has less than 5 elements, you're going to get that out of range exception. $$anonymous$$aybe you should base it off of score.Count ins$$anonymous$$d of a hardcoded 5? Just a thought, not sure what the exact needs of the script are. :)

avatar image AusieJamster · Mar 20, 2014 at 02:04 AM 0
Share

Well i need 5 score to appear with a 6th being added when the player gets a score but its never visible after its been sorted by the score value.

avatar image ShadoX · Mar 20, 2014 at 07:38 AM 0
Share

you could just use an additional variable that keeps track of how many scores you have and want to display or use

 score.Count
avatar image AusieJamster · Mar 20, 2014 at 10:29 PM 0
Share

it doesn't make sense.. I added in Debug.Log(score.Count); in the start function and it come up as 0...

Show more comments
avatar image
0

Answer by saschandroid · Mar 19, 2014 at 04:29 PM

 for (int i = 0; i < 5; i++)
 {
     score[i] = new HighscoreHolder();
     score[i].playerName = PlayerPrefs.GetString("Name" + i);
     score[i].playerScore = PlayerPrefs.GetInt("Score" + i);
 }
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 AusieJamster · Mar 20, 2014 at 02:03 AM 0
Share

I then get a "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" On that line when i add it :(

avatar image saschandroid · Mar 20, 2014 at 08:44 AM 0
Share

Then add this line:

 private static List<HighscoreHolder> score = new List<HighscoreHolder>(5);

This sets the capacity of the list to 5.

avatar image AusieJamster · Mar 20, 2014 at 10:21 PM 0
Share

Even with that i get an error on line 47 :\ it doesn't make sense.. I added in Debug.Log(score.Count); in the start function and it come up as 0...

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3 resultant array sorting 2 Answers

Make NPC travel to waypoint 1 Answer

MergeSort function acting strange 0 Answers

How to sort get components? 3 Answers

Tagged objects not returning value 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