Error when adding an item to a list
I've created a 2D List and when I try to add an item I get this error:
 ArgumentOutOfRangeException: Argument is out of range.
 Parameter name: index
 System.Collections.Generic.List`1[System.Collections.Generic.List`1[TerrainTile]].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
 WorldController.GenerateChunk (Int32 width, Int32 height, Int32 startingX, Int32 startingY, Vector2 ChunkPos) (at Assets/Scripts/World Scripts/WorldController.cs:103)
 WorldController.Start () (at Assets/Scripts/World Scripts/WorldController.cs:49)
 
               I've did as much debugging as I can think of but I cant seem to get where the problem is.
Here is my Code:
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WorldController : MonoBehaviour {
 
     /*A 2D list of tiles*/
     List<List<TerrainTile>> tiles;
 
     /*The harvestable prefab*/
     public GameObject harvGameObject;
 
     /*The world information*/
     World world;
 
     /*How big the chunks are*/
     public int ChunkWidth { get; protected set; }
     public int ChunkHeight { get; protected set; }
     
     /*Sprites*/
     public Sprite grassSprite;
     public Sprite treeSprite;
 
     /*Tile Prefab*/
     public GameObject Tile;
     
     /*Access to the player*/
     GameObject player;
     
     private void Start()
     {
         tiles = new List<List<TerrainTile>>(); //Creates new nested List
         tiles.Add(new List<TerrainTile>()); //Adds new sub List
 
         /*Right now the chunks size is put in here but will be able to be changed in the editor*/
         ChunkHeight = 30;
         ChunkWidth = 30;
 
         /*Finds the player*/
         player = GameObject.FindGameObjectWithTag("Player");
 
 
         /*Creates 100 Chunks*/
         for (int i = 0; i < 10/*FIX: Make this not a hardcoded number*/; i++)
         {
             for (int j = 0; j < 10/*FIX: Make this not a hardcoded number*/; j++)
             {
                 /*Generates A Chunk*/
 
                 GenerateChunk(ChunkWidth,/*The width of the chunk*/
                     ChunkHeight, /*Height of the chunk*/
                     i * ChunkWidth, /*The X starting position of the chunk eg: i = 2, Width = 30 therefore x = 30 * 2 finally x = 60*/
                     j * ChunkHeight, /*The starting Y position of the chunk*/
                     new Vector2(i,j)); /*The position of the chunk eg: Its the 1st chunk along and 2nd chunk up 
                 therefore the position is equal to 0,1*/
             }
         }
         //Assigns the OnPlayerMove Function to the playerMove Event
         player.GetComponent<PlayerMovement>().playerMove += OnPlayerMove;
     }
     
     void OnPlayerMove()
     {
         /*Currently Empty*/
     }
 
     /*Rounds a number to a multiple of Chunk*/
     /*eg*/
     /*RoundToChunk(10)*/
     /*10 - 10 = 0 */
     /*0 + 30 = 30*/
     /*Therefore your position was would be 0_0 */
     int RoundToChunk(int num)
     {
         num -= num % ChunkWidth;
         num += 30;
         return num;
     }
 
     public void GenerateChunk(int width, int height, int startingX, int startingY, Vector2 ChunkPos)
     {
         /*We make the width = startingX + width so the chunks will be positon correctly*/
         width = startingX + width;
         /*Make the lastTileType variable for later use*/
         Type lastTileType = new Type("Grass");
         /*We create a tile to be sized*/
         GameObject tile_go = Instantiate(Tile);
         /*We set the parent of the tile*/
         tile_go.transform.SetParent(GameObject.Find("Chunks").transform, true);
         /*We set the position of the tile*/
         tile_go.transform.position = new Vector3(startingX, startingY, 1);
         /*Now we give the chunk a name eg: 2_0 which would be the tile marked "X" in the diagram below*/
         // 0 0 0
         // 0 0 0
         // 0 0 X
         tile_go.name = "Chunk_" +ChunkPos.x +"_" + ChunkPos.y;
         /*We start a loop*/
         for (int x = startingX; x < width; x ++)
         {
             /*Now we go through all the vertical tiles in a chunk*/
             for (int y = 0; y < height; y++)
             {
                 /*Now we add a tile to the tiles list*/
                 tiles[x].Add( new TerrainTile(x, y, new Type("Grass")));
 
                 /*We check if the lastTileType is equal to this tileType if so we scale the chunk*/
                 if (lastTileType == tiles[x][y].type)
                 {
                     tile_go.transform.localScale = new Vector3(x, y, 1);
                 }
 
                 /*We set the lastTileType*/
                 lastTileType = tiles[x][y].type;
 
             }
         }
 
         /*We create the harvestables for this chunk*/
         CreateHarvestables(width, height);
     }
 
     void CreateHarvestables(int width, int height)
     {
         /*We loop through all the tiles in a chunk*/
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 /*We create a temporary gameObject for the harvestable*/
                 GameObject tempGameObject;
                 /*We see if this harvestable will be created (its a 1/99 chance)*/
                 if (UnityEngine.Random.Range(1,100) <= 1)
                 {
                     /*We instantiate the object*/
                     tempGameObject = Instantiate(harvGameObject, gameObject.transform, false) as GameObject;
                     /*We get a script off that object*/
                     HarvestableObject tempHarvScript = tempGameObject.GetComponent<HarvestableObject>();
                     /*We call a function on the new script*/
                     tempHarvScript.Created(tiles[x][y], "Tree");
                 }
             }
         }
     }
 }
 
 
               Any help would be greatly appreciated.
               Comment
              
 
               
              Your answer