help IndexOutOfRangeException

I spend 6 hours trying fix this code
but no result

help pls error:
IndexOutOfRangeException: Index was outside the bounds of the array.
TerrainGeneration.PlaceTile (UnityEngine.Sprite tileSprite, System.Int32 x, System.Int32 y) (at Assets/TerrainGeneration.cs:186)
TerrainGeneration.GenerateTerrain () (at Assets/TerrainGeneration.cs:115)
TerrainGeneration.Start () (at Assets/TerrainGeneration.cs:64)

in this video generating 5 chunks with index 0,1,2,3,4

code from this video:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TerrainGeneration : MonoBehaviour
{


    [Header("                   World Settings")]
    [Header(" ")]


    [Header("              General World Settings")]
    public float seed;
    public int worldSize = 100;
    public int treeChance = 10;
    public bool generateCaves = true;
    [Header(" ")]


    [Header("               Other World Settings")]
    public int minTreeHeight = 5;
    public int maxTreeHeight = 7;
    public int dirtLayerHeight = 5;
    public float surfaceValue = 0.25f;
    public float caveFreq = 0.08f;
    public float terrainFreq = 0.04f;
    public float heightMultiplier = 25f;
    public int heightAddition = 25;
    public int chunkSize = 16;
    [Header(" ")]

    [Header("                           Noise")]
    public Texture2D noiseTexture;
    [Header(" ")]


    [Header("                   Texture Settings")]
    [Header(" ")]
    public Sprite leaf;
    public Sprite log;
    public Sprite grass;
    public Sprite dirt;
    public Sprite stone;

    [Header("?")]
    public GameObject[] worldChunks;
    public List<Vector2> worldTiles = new List<Vector2>();

    private void Start()
    {
        if (seed == 0)
        {
            seed = Random.Range(-10000, 10000);
            GenerateNoiseTexture();
            CreateChunks();
            GenerateTerrain();

        }
        else
        {
            GenerateNoiseTexture();
            CreateChunks();
            GenerateTerrain();
        }
    }

    public void CreateChunks()
    {
        int numChunks = worldSize / chunkSize;
        worldChunks = new GameObject[numChunks];

        for (int i = 0; i < numChunks; i++)
        {
            GameObject newChunk = new GameObject();
           newChunk.name = i.ToString();
           newChunk.transform.parent = this.transform;
           worldChunks[i] = newChunk;
        }
    }

    public void GenerateTerrain()
    {
        for (int x = 0; x < worldSize; x++)
        {
            float height = Mathf.PerlinNoise((x + seed) * terrainFreq, seed * terrainFreq) * heightMultiplier + heightAddition;

            for (int y = 0; y < height; y++)
            {
                Sprite tileSprite;
                if (y < height - dirtLayerHeight)
                {
                    tileSprite = stone;

                }
                else if (y < height - 1)
                {
                    tileSprite = dirt;
                }
                else
                {
                    tileSprite = grass;
                }
                if (generateCaves)
                {


                    if (noiseTexture.GetPixel(x, y).r > surfaceValue)
                    {
                        PlaceTile(tileSprite, x, y);
                    }
                }
                else
                {
                    PlaceTile(tileSprite, x, y);
                }
                if (y >= height - 1)
                {
                    int t = Random.Range(0, treeChance);


                    if (t == 1)
                    {
                        if (worldTiles.Contains(new Vector2(x, y)))
                        {
                            GenerateTree(x, y + 1);
                        }
                    }
                }
            }
        }
    }


    public void GenerateNoiseTexture()
    {
        noiseTexture = new Texture2D(worldSize, worldSize);

        for (int x = 0; x < noiseTexture.width; x++)
        {
            for (int y = 0; y < noiseTexture.height; y++)
            {
                float v = Mathf.PerlinNoise((x + seed) * caveFreq, (y + seed) * caveFreq);
                noiseTexture.SetPixel(x, y,new Color(v, v, v));
            }
        }
        noiseTexture.Apply();
    }

    public void GenerateTree(int x, int y)
    {
        int treeHeight = Random.Range(minTreeHeight, maxTreeHeight);
        for (int i = 0; i < treeHeight; i++)
        {
            PlaceTile(log, x, y + i);
        }
        //1
        PlaceTile(leaf, x - 1, y + treeHeight);
        PlaceTile(leaf, x, y + treeHeight);
        PlaceTile(leaf, x + 1, y + treeHeight);
        //2
        PlaceTile(leaf, x - 1, y + treeHeight - 1);
        PlaceTile(leaf, x, y + treeHeight - 1);
        PlaceTile(leaf, x + 1, y + treeHeight - 1);
        //3
        PlaceTile(leaf, x - 2, y + treeHeight - 2);
        PlaceTile(leaf, x - 1, y + treeHeight - 2);
        PlaceTile(leaf, x, y + treeHeight - 2);
        PlaceTile(leaf, x + 1, y + treeHeight - 2);
        PlaceTile(leaf, x + 2, y + treeHeight - 2);
        //4
        PlaceTile(leaf, x - 2, y + treeHeight - 3);
        PlaceTile(leaf, x - 1, y + treeHeight - 3);
        PlaceTile(leaf, x, y + treeHeight - 3);
        PlaceTile(leaf, x + 1, y + treeHeight - 3);
        PlaceTile(leaf, x + 2, y + treeHeight - 3);
    }


    public void PlaceTile(Sprite tileSprite, int x, int y)
    {
        GameObject newTile = new GameObject();

        float  chunkCoord = (Mathf.Round(x / chunkSize));
        Debug.Log(chunkCoord);
        newTile.transform.parent = worldChunks[(int)chunkCoord].transform;
   

        newTile.AddComponent<SpriteRenderer>();
        newTile.GetComponent<SpriteRenderer>().sprite = tileSprite;
        newTile.name = tileSprite.name;
        newTile.transform.position = new Vector2(x + 0.5f, y + 0.5f);

        worldTiles.Add(newTile.transform.position - (Vector3.one * 0.5f));
    }
}

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is and what line of code accesses it <— (critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection size
  • remember that a collection with ZERO elements cannot be indexed at all: it is empty
  • remember you might have more than one instance of this script in your scene/prefab
  • remember the collection may be used in more than one location in the code
  • remember that indices start at ZERO (0) and go to the count / length minus 1.

This means with three (3) elements in your collection, they are numbered 0, 1, and 2 only.

1 Like

just a quick look, but when worldsize >= 96 (you set it to 100 so this would be true)

float  chunkCoord = (Mathf.Round(x / chunkSize));

will == 6, when x is greater than or equal to 96. (96/16 ==6)

your worldChunks[(int)chunkCoord] can only index from 0-5. so you need a better way to calculate the chunk coord. so it will only return 0 through to (maxChunkCoords-1). may need to adjust your worldsize to a value predictably divisible by chunksize.

1 Like