ArgumentOutOfRangeException: Argument is out of range error when used with for loop
In one one of my scripts, there is a 2D array that is iterated through every FixedUpdate()
. Another script creates the nested elements. Every time though, I get an error that says ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
.
I've confirmed over and over again that the lists inside the lists, and the objects inside those nested lists exists through Debug.Log()
. I can't figure out why the error is being thrown.
Here's the important code. The line where the error occurs is labeled:
FighterBehaviour.cs:
public class Neuron
{
public List<float> weights = new List<float>();
public float calculate(List<float> inputs)
{
float temp = 0;
for(int i = 0; i < inputs.Count; i++)
{
temp += inputs[i] * weights[i];
}
return temp;
}
public void generateWeights(float min, float max, int amount)
{
weights = new List<float>();
for (int i = 0; i < amount; i++)
{
weights.Add(Random.Range(min, max));
}
}
}
public void generateNeurons(int[] neurons)
{
for(int i = 0; i < neurons.Length; i++)
{
network.Add(new List<Neuron>());
for(int j = 0; j < neurons[i]; j++)
{
network[i].Add(new Neuron());
}
}
}
public List<List<Neuron>> network = new List<List<Neuron>>();
void FixedUpdate () {
List<float> inputs = new List<float>(4) { shotSeen, enemySeen, shotFired, FOVSize };
List<float> outputs = new List<float>();
for(int i = 0; i < network[0].Count;i++)
{
outputs.Add(network[0][i].calculate(inputs));
}
for(int i = 1; i < network.Count; i++)
{
List<float> temp2 = new List<float>();
for(int j = 0; j < network[i].Count; i++)//This is the line the error always occurs
{
temp2.Add(network[i][j].calculate(outputs));
}
outputs = new List<float>();
for(int j = 0; j < temp2.Count; j++)
{
outputs.Add(temp2[j]);
}
}
}
SimulationManager.cs: public int population = 12; public float weightRange = 2f; public int[] networkStructure;
void Start () {
bots = new FighterBehaviour[population];
for(int botIter = 0; botIter < population; botIter++)
{
bots[botIter] = Instantiate(bot);
bots[botIter].gameObject.SetActive(false);
bots[botIter].network = new List<List<FighterBehaviour.Neuron>>();
bots[botIter].generateNeurons(networkStructure);
for (int layerIter = 0; layerIter < networkStructure.Length; layerIter++)
{
for(int neuronIter = 0; neuronIter < networkStructure[layerIter]; neuronIter++)
{
if(layerIter == 0)
{
bots[botIter].network[layerIter][neuronIter].generateWeights(-weightRange, weightRange, 4);
}else
{
bots[botIter].network[layerIter][neuronIter].generateWeights(-weightRange, weightRange, networkStructure[layerIter-1]);
}
}
}
}
}
Answer by Jawchewa · May 10, 2017 at 03:54 AM
I'm not entirely sure how your code is working, but are you sure that the line with the error is supposed to be incrementing i instead of j? Are you sure on line with the error that you shouldn't be replacing i++ with j++? So something like this instead:
for(int j = 0; j < network[i].Count; j++)
I could be wrong about what you are doing, but that seems like it would make more sense with this kind of loop.
Of course it was going to be a simple typo, with two characters that look extremely similar. Thanks.
Your answer
