- Home /
Argument Out of Range On a List
Hi, I am making my own pathfinding solution to fit my game and so far it works but I just added a new function in my code and my List is not being properly adjusted. Here is the code:
void findPathOfSplitNodes()
{
splitNodesPath = new List<List<GameObject>>();
//We find the path starting at each split node and then save that path in a list
for(int i = 0; i < allSplitNodes.Count; i++)
{
splitNodesPath[i] = new List<GameObject>();
splitNodesPath.Add(goThroughPath(allSplitNodes[0]));
}
}
The problem is with splitNodesPath[i] = goThroughPath(allSplitNodes[i]);
Thanks for the help!
Answer by Kiwasi · Jun 18, 2014 at 12:22 AM
It doesn't look like you have given splitNodesPath a size. Does changing line 8 to the following still cause and error?
splitNodesPath[i] = new List<GameObject>();
If so try changing your code to
splitNodesPath.Add(goThroughPath(allSplitNodes[i]));
I updated my code to fit yours.... still does not work.
Remove line 8 that was a test to see where the error was.
If using new causes an error then replace with the add line.
Here is the code I intended you write in my answer
void findPathOfSplitNodes()
{
splitNodesPath = new List<List<GameObject>>();
//We find the path starting at each split node and then save that path in a list
for(int i = 0; i < allSplitNodes.Count; i++)
{
splitNodesPath.Add(goThroughPath(allSplitNodes[i]));
}
}
Answer by RudinUnity · Jun 18, 2014 at 02:06 AM
Hi, I have a question. Why did you initiate i-th entry of splitNodesPath in the for loop again? You have initiated splitNodesPath before the for loop. Maybe you can delete
splitNodesPath[i] = new List<GameObject>();
Probably your goThroughPath(allSplitNodes[i]) is add to the (i+1)-th entry? Or you can check i-th entry of splitNodesPath with Debug.Log to print it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Find the correct index number for array inside two loops 2 Answers
Is it possible to grab an index in a List by random as in arrays? 2 Answers
Array out of Range 1 Answer
Array index is out of Range!? 1 Answer