[Solved] Help With Procedural Generation

Hello There, I Am Currently Playing Around With Procedural Generation And I Have Hit A Brick Wall In My Code And Can’t Load Caves My Code Makes A Landscape And When You Dig It Gets Blocks From Memory And Places Then Because, I Don’t Know How To Do Chunks… So I Did It This Way But I Am Trying To Make Caves And I Am Getting This Error

IndexOutOfRangeException: Array index is out of range.
(wrapper managed-to-managed) object:ElementAddr_3_8 (object,int,int,int)
GeneratorRebuilt.DigMines (Int32 numMines, Int32 mSize) (at Assets/GeneratorRebuilt.cs:100)
GeneratorRebuilt.Generate () (at Assets/GeneratorRebuilt.cs:63)
GeneratorRebuilt.Start () (at Assets/GeneratorRebuilt.cs:45)

Can Someone Please Help This Is My Code

using UnityEngine;
using System.Collections;

public class Block {
    public int type;
    public bool vis;
    public GameObject block;

    public Block(int t, bool v, GameObject b) {

        type = t;
        vis = v;
        block = b;
    }
}

public class GeneratorRebuilt : MonoBehaviour {

    public static int width = 128;
    public static int depth = 128;
    public static int height = 200;
    public int heightScale = 20;
    public int heightOffset = 100;
    public float detailScale = 25.0f;
    public int sandPlacement = 110;
    public int grassPlacement = 117;
    public int dirtPlacement = 114;
    public int stonePlacement = 0;


   
    public GameObject grassBlock;
    public GameObject dirtBlock;
    public GameObject stoneBlock;
    public GameObject sandBlock;
    public GameObject cloudBlock;
    public GameObject DiamondOreBlock;
    public GameObject BedrockBlock;

    Block[,,] worldBlocks = new Block[depth,height,width];


    // Use this for initialization
    void Start () {
        Generate ();
    }
    void Generate() {
        int seed = (int)Network.time * 10;
        for(int z = 0; z < depth; z++) {
            //Setting Custom Seed
            for(int x = 0; x < width; x++) {
                int y = (int) (Mathf.PerlinNoise((x+seed)/detailScale, (z+seed)/detailScale) * heightScale) + heightOffset;
                Vector3 blockPos = new Vector3(x,y,z);
                CreateBlock (y, blockPos, true);
                while(y > 0) {
                    y--;
                    blockPos = new Vector3 (x,y,z);
                    CreateBlock (y, blockPos, false);
            }
        }
    }
        DrawClouds (30, 150);
        DigMines (6, 500);
        Debug.Log ("The Current Level Seed: "  + seed);
}

    void DrawClouds(int numClouds, int cSize) {
        for(int i = 0; i < numClouds; i++) {
            int xpos = Random.Range(0, width);
            int zpos = Random.Range (0, depth);

            for (int j = 0; j < cSize; j++) {
                Vector3 blockPos = new Vector3 (xpos, height - 1, zpos);
                GameObject newBlock = (GameObject) Instantiate (cloudBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (5, true, newBlock);
                xpos += Random.Range (-1, 2);
                zpos += Random.Range (-1, 2);
                if (xpos < 0 || xpos >= width) xpos = width / 2;
                if (zpos < 0 || zpos >= depth)zpos = depth / 2;
            }
        }
    }

    void DigMines(int numMines, int mSize) {
           
        int holeSize = 2;
        for (int i = 0; i < numMines; i++) {
            int xpos = Random.Range(10, width-10);
            int ypos = Random.Range(10, height-10);
            int zpos = Random.Range (10, height-10);
            for (int j = 0; j < mSize; j++) {
                    for (int x = -holeSize; x <= holeSize; x++)
                   
                        for(int y = -holeSize; y <= holeSize; y++)
                       
                            for(int z = -holeSize; z <= holeSize; z++) {
                           
                                if(!(x == 0 && y == 0 && z == 0)) {
                                    Vector3 blockPos = new Vector3(xpos+x, ypos+y, zpos+z);
                                    if(worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] != null)
                                        Destroy(worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].block);
                                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = null;
                    }
                }

                xpos += Random.Range (-1, 2);
                ypos += Random.Range (-1, 2);
                zpos += Random.Range (-1, 2);
                if (xpos < holeSize || xpos >= width - holeSize) xpos = width / 2;
                if (ypos < holeSize || ypos >= height - holeSize) ypos = height / 2;
                if (zpos < holeSize || zpos >= depth - holeSize) zpos = depth / 2;
            }
        }
        for(int z = 1; z < depth-1; z++) {
            for (int x = 1; x < width-1; x++) {
                for (int y = 1; y < depth-1; y++) {
                    if (worldBlocks [x, y, z] == null) {
                        for (int x1 = -1; x1 <= 1; x1++)
                            for (int y1 = -1; y1 <= 1; y1++)
                                for (int z1 = -1; z1 <= 1; z1++) {

                                    if (!(x1 == 0 && y1 == 0 && z1 == 0)) {
                                        Vector3 neighbour = new Vector3 (x+x1, y+y1, z+z1);
                                        DrawBlock (neighbour);
                            }
                        }
                    }
                }
            }
        }
    }

        void CreateBlock(int y, Vector3 blockPos, bool create) {

        GameObject newBlock = null;
        if (y > grassPlacement) {
            if(create)
                newBlock = (GameObject) Instantiate (grassBlock, blockPos, Quaternion.identity);
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (1, create, newBlock);
        } else if (y > dirtPlacement) {
                    if(create)
                            newBlock = (GameObject) Instantiate (dirtBlock, blockPos, Quaternion.identity);
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (2, create, newBlock);
            } else if (y > sandPlacement) {
                        if(create)
                            newBlock = (GameObject) Instantiate (sandBlock, blockPos, Quaternion.identity);
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (3, create, newBlock);
                } else
                    if (y > stonePlacement) {
                            if(create)
                                newBlock = (GameObject)    Instantiate (stoneBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (4, create, newBlock);
            } else if(y > 70 && y < 80 && Random.Range(0,100) < 3) {
                if(create)
                    newBlock = (GameObject) Instantiate (DiamondOreBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (5, create, newBlock);
            } else if(y > 1 && y < 5 && Random.Range(0,100) < 90) {
                if(create)
                    newBlock = (GameObject) Instantiate (BedrockBlock, blockPos, Quaternion.identity);
                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (6, create, newBlock);
        }
    }

    void DrawBlock(Vector3 blockPos) {

        if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] == null) return;

        if (!worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis) {
            GameObject newBlock = null;
            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = true;

                if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 1)
               
                newBlock = (GameObject) Instantiate (grassBlock, blockPos, Quaternion.identity);
           
            else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 2)
               
                newBlock = (GameObject) Instantiate (dirtBlock, blockPos, Quaternion.identity);
           
        else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 3)
               
                newBlock = (GameObject) Instantiate (sandBlock, blockPos, Quaternion.identity);
           
    else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 4)
               
                newBlock = (GameObject) Instantiate (stoneBlock, blockPos, Quaternion.identity);
           
    else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 5)

                newBlock = (GameObject) Instantiate (DiamondOreBlock, blockPos, Quaternion.identity);
    else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 6)

                newBlock = (GameObject) Instantiate (BedrockBlock, blockPos, Quaternion.identity);
    else
        worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = false;
        }
    }
       
   
    // Update is called once per frame
    void Update() {
        if (Input.GetMouseButtonDown (0)) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2.0f, Screen.height / 2.0f, 0));
            if(Physics.Raycast(ray, out hit, 3.0f)) {
                Vector3 blockPos = hit.transform.position;

                if ((int)blockPos.y == 0) return;

                worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = null;

                Destroy(hit.transform.gameObject);

                for (int x = -1; x <= 1; x++)
                    for (int y = -1; y <= 1; y++)
                        for (int z = -1; z <= 1; z++) {

                            if(!(x == 0 && y == 0 && z == 0)) {
                                Vector3 neighbour = new Vector3(blockPos.x+x, blockPos.y+y, blockPos.z+z);
                                DrawBlock(neighbour);
                    }
                }
            }
        }
    }
}

Just a cursory glance and I saw this:

int zpos = Random.Range(10, height - 10);

I’m sure you meant it to be:

int zpos = Random.Range(10, depth - 10);

Well I Guess Your Right, The Stupid Tutorial I Was Watching Was Saying Change It Into “height” I don’t honestly Know Why

It will definitely overflow since depth is 128 and height is 200… you worldblocks only have 128 depth so if zpos is ever 128-199 you get the error.

Thanks

Also this line is going to cause you problems:

Block[,,] worldBlocks = new Block[depth,height,width];

Everywhere your code access worldBlocks by worldBlocks[x,y,z] Not [z,y,x]. The only reason you haven’t had a million errors is you’ve also coincidentally defined width and depth to 128. But as soon as you change those number to not be the same your code will break.

Change it to :

Block[,,] worldBlocks = new Block[width,height,depth];