- Home /
Function throws 'argument out of range, parameter: index' error
Hi, I'm trying to include a clumsy autocomplete text feature in my game, and the function that should return the suggested words in a list produces the above error when I Debug.Log it.
#pragma strict
import System.Collections.Generic;
public var stringToEdit : String = "";
var verblist = new List.<String>();
verblist.Add("look");
verblist.Add("swim");
verblist.Add("loom");
verblist.Add("lecture");
public var words = new List.<String>(); //just showing the initialization
stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);
//in the OnGUI()
function getWord(){
for (var i : int = 0; i < verblist.Count; i++){
var j = -1;
var correct = 1;
var enteredText = stringToEdit.length;
while (correct == 1 && j++ < enteredText){
if (verblist[i].Substring(j,1) != stringToEdit.Substring(j))
{
correct = 0;
}
}
if (correct == 1)
{
words.Add(verblist[i]);
}
}
Debug.Log(words[0]);
return words;
}
It's clearly not adding to words, because I'm logging index 0. Any ideas why? Thanks in advance
This bit:
verblist.Add("look");
verblist.Add("swim");
verblist.Add("loom");
verblist.Add("lecture");
$$anonymous$$ove that into an Awake()
or Start()
function, so that it'll be executed once your object spawns.
Aside from pretty basic default values for your variables, you usually don't want (or can't have) code that roams wild without being inside a function.
I'm hesitant to let this through the moderation queue because it's a frequently asked question. It's not a bad question, we're just trying to cut down on the number of duplicate threads in this area of the site. If you have anything else to add, feel free to comment here; if not, I'll drop the question from the queue in a little while.
If you can't find help, here, you can always try the forums or look up some tutorials online.
I appreciate it, and sorry that I may have missed the FAQ within. Please note, though, that the issue was not with populating 'verbslist' but 'words' in the match-er function. I was able to get it working, but the new hurdle is that the function is either being called twice, or doubling itself:
function getWord(){
for (var i : int = 0; i < verblist.Count; i++){
var j = -1;
var correct = 1;
while (correct == 1 && ++j < stringToEdit.length){
if (verblist[i].Substring(j,1) != stringToEdit.Substring(j,1))
{
correct = 0;
}
}
if (correct == 1)
{
words.Add(verblist[i]);
matchCounter++;
Debug.Log(matchCounter);
}
}
Debug.Log(words[0]);
return words;
}
For instance, this logs '1 2 3 4 look' if 'lo' is in the textfield (i.e. it's matching 'look' and 'loom' twice, then logging the first match). I've tried calling it from a keypress and as a start, and still it doubles the matches.
Don't send a while loop to do the job of a for loop.
You code will be much easier to read and debug if you use the proper loops in their proper places.
Also will work better if you tell us which line is throwing the error.
function getWord()
{
for (var i : int = 0; i < verblist.Count; i++)
{
var j = -1;
var correct = 1;
while (correct == 1 && ++j < stringToEdit.length)
{
if (verblist[i].Substring(j,1) != stringToEdit.Substring(j,1))
{
correct = 0;
}
}
if (correct == 1)
{
words.Add(verblist[i]); //this is where the word is matched
matchCounter++; //this is being triggered twice as much as it should
Debug.Log(matchCounter);
}
}
Debug.Log(words[0]); //shows first match
return words;
}
Definitely would rather not nest a while in a for, but can I still write a for that checks character by character until it finds one != then stops?
Your answer
![](https://koobas.hobune.stream/wayback/20220613160247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Cycling through a List with constantly changing element count. Running into problems D: 2 Answers
Arguement out of range exception on a lists index 3 Answers
Runtime IndexOutOfRangeException when testing on iPhone 1 Answer
List of type Button has size set to zero at runtime 0 Answers