- Home /
index out of range with length as for loop param
I am trying to loop through some inputs that are given to the function as an array of floats. I use a for loop to do so with the length of the array as the parameter. I don't know how but it seems that the index still somehow gets out of bounds...
using UnityEngine;
using System.Collections;
public class Neuron {
public float[] weights=new float[100];
public float calculate(float[] inputs){
float activation = 0f;
for(int i=0;i<inputs.Length;i++){
activation+=inputs[i]*weights[i];
}
//bias
activation += -1 * weights [weights.Length - 1];
//Sigmoid
return (1 / (1 + Mathf.Exp(-activation/0.5f)));
}
public void setWeights(float[] wts){
weights = wts;
Debug.Log(wts.Length);
}
}
With the error:
IndexOutOfRangeException: Array index is out of range.
Neuron.calculate (System.Single[] inputs) (at Assets/Neuron.cs:12)
how come the error points at line 12 but there is a comment there? anyway I bet 2 cents the error is at line 10 and the problem is the inputs are more that 100.
Sorry, I took out a piece for testing but put in the previous error report. The error was now at Line 10. I found that @$$anonymous$$iraSensei was correct in that the weights was being set to an array of Length 0 in another class.
Answer by KiraSensei · Jan 06, 2015 at 08:44 AM
Are you sure "weights" is at least as long as "inputs" ?
Your answer
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
New Input System: Can't set binding path anymore 2 Answers
what are the single joystick prefab's inputs for the input manager 1 Answer
Input System multiple actions on same keybind 0 Answers
Using DPad/Joystick in "new" InputSystem as digital rather than analog 1 Answer