- Home /
Why does it give me this error and how can i fix it?
So im attempting to learn how to use for loops and arrays together but i cant seem to figure out this error it keeps giving me. The error says "IndexOutOfRangeException. Array index is out of range". The error is on line 52, the one i put stars next to. The script im using is:
var numberInventory : int[];
private var number : int = 0;
var showInventory = false;
var xStart : float;
var yStart : float;
var buttonSize = 80;
function Start ()
{
numberInventory = new int[number];
xStart = Screen.width/2-200;
yStart = Screen.height/2-200;
}
function Update ()
{
if (Input.GetKeyDown("e"))
{
if (!showInventory)
{
showInventory = true;
}else{
showInventory = false;
}
}
}
function OnGUI ()
{
if (GUI.Button(Rect(0,0,100,100), "ADD"))
{
number ++;
numberInventory = new int[number];
}
if (showInventory)
{
GUI.Box(Rect(xStart, yStart-20, 400, 420), "Inventory");
for (var x=0; x<5; x++)
{
for (var y=0; y<5; y++)
{
****if (GUI.Button(Rect(xStart+buttonSize*x, yStart+buttonSize*y, buttonSize, buttonSize), ""+numberInventory[number]))****
{
}
}
}
}
}
How can i fix this? Thanks!
Answer by Chronos-L · Apr 06, 2013 at 04:07 AM
Index problem.
A array with size n
will have n
elements and the indexes of these elements are { 0, 1, ... n-1}.
You declare your array as:
numberInventory = new int[number];
So the index should be in { 0, 1, ..., number - 1 }. Using numberInventory[number]
will produce a index out of range exception.
var index = 0;
for (var x=0; x<5; x++)
{
for (var y=0; y<5 && index < number; y++)
{
if (GUI.Button(Rect(xStart+buttonSize*x, yStart+buttonSize*y, buttonSize, buttonSize), ""+numberInventory[index++]))
{
}
}
}
Usually in program$$anonymous$$g, zero-based indexing is used.
Index can be see as the offset of the memory. In memory, arrays are stored as a large block of memory, each smaller block is an element and their have a definite size.
[ ][ ][ ][ ][ ][ ]...
In C, we refer to an array using pointer. Lets say the location of a int
array in the memory is at 0F4A (random hexadecimal), then the first value will be at OF4A, the second value will be at ( OF4A + 1 memory-size-of-int ), and the third value will be at ( 0F4A + 2 memory-size-of-int ), so on and so forth. The shorthad way of writing that would be arr[x]
where x
is the offset of the memory pointer.
Another way to see it is, there are 10 person standing in a line and they are 1 meter apart from each other. The first person will stand at X
, then the second person will be standing at X + 1
, the n
th person would be standing at X + (n-1)
.
So the first element is arr[0]
, the second element is arr[1]
, so on and so forth. You can see this in arithmetic progression as well: a + (n-1)d
, when you are the first one, n = 1, so you will get a + ( 1 - 1 )d
, which is a
.
This all may seems counter-intuitive, because in daily life we count in { 1, 2 ,3 .... }, but it will make more sense when you get used to it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Inventory Help. 2 Answers
Where can i begin to learn how to make an inventory. (Javascript). 1 Answer
Saving Certain Vector3's positions 2 Answers