IndexOutOfRangeException: Array index is out of range? Someone knows what bug is..?

I’ve been tested for a day. it gaves me errors always. But i guess, there is nothing wrong for code. What i did is this.

//!------------------------------------------------------------------------------------------------
class MALLOCK {
private var WIDTH : int = 10 + 1; // width of board
private var HEIGHT : int = 20 + 1; // height of board

function MALLOCK() {

position = new _blockModuler();
block = new GameObject[4];
block[0] = block[1] = block[2] = block[3] = null;

aiCheckBoard = new int[WIDTH * HEIGHT];
if(aiCheckBoard == null)
Debug.Log(“allocation is something wrong.”);

for(var i : int = 0; i < WIDTH * HEIGHT; i++) {
aiCheckBoard = 0;

  • }*

  • Debug.Log(“allocation is done.”);*

  • }*

  • var aiCheckBoard : int[ ]; // check board;*

  • var position : _blockModuler;*

  • var block : GameObject[ ];*
    };
    then i used it outside as
    for(var i : int = 0; i < 10; i++) {
    Debug.Log("board: " + __alloc.aiCheckBoard[0] );
    }
    It always said that message as i posted.
    IndexOutOfRangeException: Array index is out of range.
    Did i something missed?
    Thanks.

That means you are trying to access a member of the array that doesn’t exist.

So you have an array that has, for example, 10 members in it. You are trying to access the 11th member, that doesn’t exist, and the index is therefore, out of range.

^ axis == access.

There’s nothing wrong with that code. Something else is causing the problem.

If you double-click the error it’ll open the script editor to the offending line.

Just debug the length of the array and presto, you see that it’s not a bug.

OK, here is how i solved the issue. In my game i was using 6 different backgrounds and i only had 5 set up.

Go to you heirachy and select background.

In your inspector go to background tan and check how many pictures you have, change the size depending on how many pictures you are using for backgrounds.