IndexOutOfRangeException: Array index is out of range

IndexOutOfRangeException: Array index is out of range.
(wrapper managed-to-managed) object:ElementAddr (object,int,int,int)
WorldScript.Awake () (at Assets/Resources/KeenansCoolResources/KeenansCoolScripts/MeshModifyingAndGenerating/Voxel Based/3D/WorldScript.js:41)

So i know what it means…I didn’t come straight to post a thread before looking up there error first. Still! I can’t figure out where the code is trying to grab something that doesn’t exist in the array. I’d appreciate some help on this one :slight_smile: Here’s my code

#pragma strict

@script AddComponentMenu ("Cool Tools/Mesh Modification And Generation/3D/World (Chunk Manager)")

//Inspector Variables-----------------------------------------------------
var chunk : GameObject;
var chunks : GameObject[,,];
var chunkSize : int = 16;

var data : byte[,,];//This array is the center of the script and is very important
var worldX : int = 16;
var worldY : int = 16;
var worldZ : int = 16;

//Hidden Variables--------------------------------------------------------


function Awake () 
{
    //This initiates the data array and makes it the size of the world
    //By default this will only make up one chunk, a 16x16x16 group of blocks
    data = new byte[worldX, worldY, worldZ];

    //Now we will cycle through every block that makes up the chunk and modify it based on certain variables
    for(var x : int = 0; x < worldX; x++)
    {
        for(var z : int = 0; z < worldY; z++)
        {
            var stone : int = PerlinNoise(x, 0, z, 10, 3, 1.2);
            stone += PerlinNoise(x, 300, z, 20, 4, 0) + 10;
            var dirt : int = PerlinNoise(x, 100, z, 50, 2, 0) + 1;//We add one so that there the minimum depth of grass is one
            for(var y : int = 0; y < worldZ; y++)
            {
                //If the current block the code is looking at is below 9, it will be a stone block
                if(y <= stone)
                {
                    data[x, y, z] = 1;//Here we set the block to be stone (0 is air, 1 is stone, 2 is dirt)
                }
                else if(y <= dirt + stone)
                {
                    //ERROR BRINGS ME TO THE LINE BELOW THIS COMMENT------------------------------------------
                    data[x, y, z] = 2;
                }
            }
        }
    }

    chunks = new GameObject[Mathf.FloorToInt(worldX / chunkSize), Mathf.FloorToInt(worldY / chunkSize), Mathf.FloorToInt(worldZ / chunkSize)];

    //This for-loop 
    for(x = 0; x < chunks.GetLength(0); x++)//While x is less than the first value (default 8) in the chunks array declared above
    {
        for(y = 0; y < chunks.GetLength(1); y++)
        {
            for(z = 0; z < chunks.GetLength(2); z++)
            {
                chunks[x, y, z] = Instantiate(chunk, new Vector3(x * chunkSize, y * chunkSize, z * chunkSize), new Quaternion(0,0,0,0));
                var newChunkScript : ChunkScript = chunks[x, y, z].GetComponent(ChunkScript);

                newChunkScript.worldGameObject = gameObject;
                newChunkScript.chunkSize = chunkSize;
                newChunkScript.chunkX = x * chunkSize;
                newChunkScript.chunkY = y * chunkSize;
                newChunkScript.chunkZ = z * chunkSize;
            }
        }
    }
}

function PerlinNoise (x : int, y : int, z : int, scale : float, height : float, power : float) : int
{
    var rValue : float;
    rValue = Noise.GetNoise(x / scale, y / scale, z / scale);
    rValue *= height;
    if(power != 0)
    {
        rValue = Mathf.Pow(rValue, power);
    }
    return rValue;
}

function Block (x : int, y : int, z : int) : byte
{
    if(x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z < 0)
    {
        return 1;
    }
    return data[x, y, z];
}

for(var z : int = 0; z < worldY; z++)

Also the Y loop.

–Eric