- Home /
Array index is out of range i am getting this error Please help me
var i:int;
var move:GameObject[];
move=GameObject.FindGameObjectsWithTag("Player");
var screenPoint:Vector3;
var offset:Vector3;
var curScreenPoint:Vector3;
var curPosition:Vector3;
function OnGUI()
{ if(GUI.Button(Rect(0,60,50,30),"Explore")) { for(i=0;i<=move.Length;i++)
{
screenPoint = Camera.main.WorldToScreenPoint(move[i].gameObject.transform.position);
offset = move[i].gameObject.transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
//function OnMouseDrag() //{ curScreenPoint = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); transform.position = curPosition;
//} } }
Answer by hirenkacha · Nov 19, 2013 at 09:32 AM
Array's length is always the last index +1. If you try accessing the last element of an array it should be [Length-1] and NOT [Lenght]. For Eg. For an array 'a' having 10 elements, a.Length = 10;
a[0] = .. //Length=1
a[1] = .. //Length=2
.
.
a[9] = .. //Length=10
a[10] = null
So you need to change your for loop to
for(i=0;i<move.Length;i++)
this will solve the problem.
Answer by KiraSensei · Nov 19, 2013 at 08:55 AM
Try to loop like that :
for (int i=0; i<move.Length; i++)
You are going too far.
Your tags are useless, it exists to make categories of questions, and by definition anyone who asks a question needs help.
And please next time give the error line and properly format your code, some people who can help you won't just because it is unreadable.
Answer by Hoeloe · Nov 19, 2013 at 09:00 AM
You're not quite understanding how arrays are structured. An IndexOutOfRange exception is thrown when you try to access part of an array that does not exist. For example, if I have an array with 5 elements in it, and I try to access the 6th, it will give me the OutOfRange exception, because there is no 6th element.
Now, you also need to consider that arrays are what we call 0-indexed. This means that, when accessing an array, to get the first element in the array, you ask for index 0 (that is, array[0]
will give you the first element. This has some implications that you haven't quite picked up on, that I will try and explain with an example. Let's say I have an array of length 6 (that means there are 6 elements in the array, and array.Length
is equal to 6). It looks like this:
{ a, b, c, d, e, f }
Now, I'll annotate this with the index you need to call to access the specific element of the array:
i = 0 1 2 3 4 5
{ a, b, c, d, e, f }
So if we call array[i]
we will get the corresponding item in the array when i is any of the values seen there. The implication here is something you might have spotted already. The array contains 6 elements. The length of the array is 6. But, because the first element is indexed with 0, the last is indexed with 5, not 6. Zero indexing means the last element of an array can be gained like this: array[array.Length
-1
]
.
Now, let's have a look at your for loop:
for(i=0;i<=move.Length;i++)
Now, this means that i will progress from 0 to the length of the array move
, inclusive. Let's assume move has 3 elements. On the first run on this loop, i will be 0, so calling move[i]
equates to move[0]
, which, as we've said, is the first element of the array.
On the next iteration, move[i]
equates to move[1]
, which is the second element.
On the third iteration, move[i]
becomes move[2]
, which is the third element of the array.
Now, at the start of the next iteration, i is equal to 3, so we check the condition: i<=move.Length
, and find that 3move[i] becomes move[3]
, which gets the fourth element of the array... but the array only has 3 elements, so there is no fourth element to get. This causes the program to throw and OutOfRange exception, to prevent you from accessing uninitialised spaces in memory.
Hopefully this has helped you to understand what is causing the problem, and if you've read this through properly, you'll be able to fix it.