- Home /
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;
}
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 :)
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.
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. :)
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.
you could just use an additional variable that keeps track of how many scores you have and want to display or use
score.Count
it doesn't make sense.. I added in Debug.Log(score.Count); in the start function and it come up as 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);
}
I then get a "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" On that line when i add it :(
Then add this line:
private static List<HighscoreHolder> score = new List<HighscoreHolder>(5);
This sets the capacity of the list to 5.
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
Follow this Question
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