Index out of range why? (possible bug)
Ingame the script will not run as the index is out of range on this script. It is only occurring on one line.
public class AIController : MonoBehaviour {
float tlu;
float tld;
int RibI;
public float[] tibl;
int RprsdI;
public float[] tprsdl;
public int whichOne = 0;
public bool record;
void Update () {
float timer = 0;
bool pressing = false;
if (record) {
tlu += Time.deltaTime;
if (Input.GetKeyDown ("right")) {
tibl [RibI] = tlu;
RibI++;
}
if (Input.GetKey ("right")) {
tld += Time.deltaTime;
}
if (Input.GetKeyUp ("right")) {
tprsdl [RprsdI] = tld;
RprsdI++;
tld = 0;
tlu = 0;
}
}
if (!record) {
timer += Time.deltaTime;
//this line
if (timer >= tibl[0]) {
transform.Translate (1, 0, 0);
timer = 0;
pressing = true;
}
if(timer >= tprsdl[whichOne] && pressing) {
whichOne++;
pressing = false;
timer = 0;
}
}
}
}
I honestly don't know how, I think it is something that i did. It occurs on that line and only that line, and yes i did set the values of the array to a number higher than zero and it still did not work. I appreciate the help.
Answer by cjdev · Feb 03, 2016 at 03:42 AM
When using an array you have to instantiate it first with the maximum number of elements it can have like this:
float[] tibl = new float[10];
I am still getting the same error, I think I am not going to use this script anyway as it doesn't work the way i hoped.
Oddly enough the solution was to change the bool (!record) to something completely different. If it is a bug, i am not sure but it sure is strange.