0
\$\begingroup\$

I'm a bit confused here. When the const int chunkNum is highter than 9, it give me the error and instead of getting 10 x 10 chunks, i get 10x 9 chunks, but for a value of 9 i get 9x9. Thats the error I get:

IndexOutOfRangeException: Index was outside the bounds of the array. biome.GenerateChunk (System.Int32[] Y_VERTICES, UnityEngine.GameObject chunk) (at Assets/Script/biome.cs:108)

But for values from 1 to 9, its fine. I don't see the reason for it to happen. The error is in the "création des vecteurs" loop, at the end.

    public float CUBE_SZE = 1;
const int seed = 50;
const int FREQUENCE = 100;
const int AMPLITUDE = 100;
public int size = 50;
const int chunkNum = 10;
int[] BIOME_DATA = { 0, 33, 66, 25, 58, 91, 50, 83, 116, 75, 108, 141 };

int[] temp_VERT;
int[] hum_VERT;
int[] biome_VALUE;

List<GameObject> chunks = new List<GameObject>();
public GameObject chunky;

void Update()
{
    if (Input.GetKeyDown("r"))
    {
        GameObject[] c = GameObject.FindGameObjectsWithTag("Chunk");
        foreach(GameObject ch in c)
        {
            Destroy(ch.gameObject);
        }
        Generate();
    }
}

void Generate()
{
    temp_VERT = new int[Mathf.FloorToInt(size / CUBE_SZE) * Mathf.FloorToInt(size / CUBE_SZE)];
    hum_VERT = new int[Mathf.FloorToInt(size / CUBE_SZE) * Mathf.FloorToInt(size / CUBE_SZE)];
    biome_VALUE = new int[Mathf.FloorToInt(size/CUBE_SZE) * Mathf.FloorToInt(size/CUBE_SZE)];
    for (int x = 0; x < chunkNum; x++)
    {
        for (int z = 0; z < chunkNum; z++)
        {
            GameObject chunk = Instantiate(chunky, new Vector3(x * (size*CUBE_SZE), 0, z * (size*CUBE_SZE)), Quaternion.identity);
            chunks.Add(chunk);
            GenerateChunk(chunk);
        }
    }

}

void GenerateChunk(GameObject chunk)
{
    Vector3 CHUNK_POSITION = new Vector3(Mathf.Round(chunk.transform.position.x),0, Mathf.Round(chunk.transform.position.z));

    //generate the y values for the humidity vertices
    for (float x = CHUNK_POSITION.x, i = 0; x < size + CHUNK_POSITION.x; x++)
    {
        for (float z = CHUNK_POSITION.z; z < size + CHUNK_POSITION.z; z++)
        {
            float y = Mathf.PerlinNoise((float)(x + seed) / FREQUENCE, (float)z / FREQUENCE) * AMPLITUDE;
            y = Mathf.Floor(y / 33) * 33;
            hum_VERT[(int)i] = (int)y;
            i++;
        }
    }

    //generate the y values for the temperature vertices
    for (float x = CHUNK_POSITION.x, i = 0; x < size + CHUNK_POSITION.x; x+=CUBE_SZE)
    {
        for (float z = CHUNK_POSITION.z; z < size + CHUNK_POSITION.z; z+=CUBE_SZE)
        {
            float y = Mathf.PerlinNoise((float)(x - seed) / FREQUENCE, (float)z / FREQUENCE) * AMPLITUDE;
            y = Mathf.Floor(y / 25) * 25;
            temp_VERT[(int)i] = (int)y;
            i++;
        }
    }

    //assigne une valeur représentant un biome pour chaque vertex 
    for (int i = 0; i < biome_VALUE.Length; i++)
    {
        biome_VALUE[i] = System.Array.IndexOf(BIOME_DATA, (hum_VERT[i] + temp_VERT[i]));
        //biome_VALUE[i] = new Vector2(temp_VERT[i],hum_VERT[i]);
    }

    GenerateChunk(biome_VALUE,chunk);
}

void GenerateChunk(int[] Y_VERTICES, GameObject chunk)
{
    Mesh meshy;
    MeshRenderer meshRenderer = chunk.GetComponent<MeshRenderer>();
    meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
    MeshFilter meshFilter = chunk.GetComponent<MeshFilter>();

    Vector3[] VERTICES = new Vector3[Mathf.FloorToInt(size / CUBE_SZE) * Mathf.FloorToInt(size / CUBE_SZE)];
    List<Vector2> UV = new List<Vector2>();
    List<int> TRIANGLES = new List<int>();
    List<Vector3> NORMALS = new List<Vector3>();

    meshy = new Mesh();
    //création des Vecteur
    for (float i = 0, x = 0; x < size; x+= CUBE_SZE)
    {
        for (float z = 0; z < size; z+=CUBE_SZE)
        {
            VERTICES[(int)i] = new Vector3(Mathf.Round(x), Mathf.FloorToInt(BIOME_DATA[Y_VERTICES[Mathf.FloorToInt(i)]]), Mathf.Round(z));
            i++;
            

        }
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ You have 5 array accesses that I can see in that function, and three in the vector creation loop. Which one throws the exception? What index are you trying to look up when it occurs? Have you tried setting a debugger and stepping through this function with a debugger to find out where its behaviour departs from what you expect it to do? \$\endgroup\$ – DMGregory Jan 8 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.