- Home /
Array Is Out Of Range
I'm using a while loop to find the last open spot in a string array with 10 elements. Since it is an array, I can't just add a string to the end of it, so what I did was create the array with the max number of spots it can have, and whenever I needed to add a string to it, I would use this code to find the last open spot. I would then make that last open spot part of the array:
void SearchLastSpot () {
int i = 0;
bool searchLength = true;
Debug.Log(i);
while(searchLength){
if(userQuotes[i] == ""){
searchLength = false;
lastOpenSpot = i;
}else{
searchLength = true;
}
i++;
}
Debug.Log(lastOpenSpot);
}
And later on in the code:
if(GUI.Button(new Rect(Screen.width/2, Screen.height/1.18F, 100, 50), "Done")){
SearchLastSpot();
userQuotes[lastOpenSpot] = newQuotes;
userTypes[lastOpenSpot] = newQuotesType;
PlayerPrefsX.SetStringArray("User Quotes", userQuotes);
PlayerPrefsX.SetStringArray("User Quote Type", userTypes);
PlayerPrefs.SetInt("LastOpenSpot", lastOpenSpot);
}
So thats how I'm using it. I'm getting an ArrayIndexIsOutOfRange error for the line:
if(userQuotes[i] == ""){
Which makes no sense cause I did a debug and i = 0, and there's more than 1 element in my array. What am I doing wrong here?
$$anonymous$$ove the debug to the first line of your while loop. It might be overstepping the bounds after a few runs. See if it is giving a value of i that is beyond the limit there.
Nope, no matter what I make i, or what the size of the array is, it keeps returning the same number.
I even wrote another script a couple $$anonymous$$utes ago that uses this while loop:
wp = GameObject.FindGameObjectsWithTag("InWaypoint");
while(i < wp.Length){
Debug.DrawLine(wp[i].transform.position, wp[a].transform.position);
i++;
a++;
}
And it still gives me that same error. Its in a completely different script! And thats all thats in the other script.
I don't see the definitions of these arrays and are you sure that a isn't exceeding the bounds in the second example. I'd highly recommend using List\<\> rather than arrays too.
Your first loop may well have a problem because you are checking for "" but the entries may be null - the normal way of handling that would be to do:
if(string.IsNullOrEmpty(userQuotes[i]))
$$anonymous$$aybe reinventing the wheel is what you are doing wrong.
There are many collections (List, LinkedList, Dictionary...) that may fit better your particular use.
Answer by Wolfram · Jul 04, 2012 at 12:55 PM
Note that once your string array is already filled, searchLength will never be set to false before you run out of available slots in your array, thus causing your error. You need to make sure you never access elements with an index >= userQuotes.Length, and catch the case where you used up all available spots (because your array is full).
The Debug.Log(i); will of course always print "0", since you put this line directly after you initialized i with 0.
Concerning your other script posted in the comment, your "i" is not the problem, your "a" is. change the condition to
while(i < wp.Length && a < wp.Length ){
Thanks, this solves the problem. I was confused because that script worked before with no problems, but then I combined two scripts together so that I didn't have to use GetComponent or use static variables and then it didn't work. But this solves the problem, thanks.
Answer by nventimiglia · Jul 04, 2012 at 12:45 PM
Just use a List.
Trust me, if I had the option to use a list, I would have.
Your answer
Follow this Question
Related Questions
IndexOutOfRangeException: Array index is out of range. 1 Answer
Split each number in an int without converting to char and string first? 1 Answer
Why is this giving me an error? (ToBuiltin problems) 1 Answer
Finding the number of elements in a sub-array? 1 Answer
Index out of range inside IEnumerator 0 Answers