- Home /
For loops / lists / instantiating not working! D:
I've been having trouble with instantiating and for loops.
I have been working on a game where the player puts in coordinates for a grid and game objects are instantiated around the lengths of the grid. I have a piece of code which is NOT working and I've been stuck on it for about a week now, around a for loop based on the count of a list. I'll show the code and arguments of it down below:
( The problem is with PlaceWall() and PlaceFloor() )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorGenerator : MonoBehaviour {
public GameObject floor;
public GameObject wall;
public GameObject landParent;
List<Vector2> possibleFloorAreas = new List<Vector2>();
public List<Vector2> possibleWallAreas = new List<Vector2>();
List<GameObject> tileSets = new List<GameObject>();
public void Start()
{
DiscoverAreas();
}
public void DiscoverAreas()
{
possibleFloorAreas.Clear();
for (int r = 0; r <= CoinGenerator.instance.rows; r++)
{
for (int c = 0; c <= CoinGenerator.instance.collumns; c++)
{
possibleFloorAreas.Add(new Vector2(c, r));
}
}
possibleWallAreas.Clear();
for (int u = -1; u <= CoinGenerator.instance.collumns + 1; u++)
{
possibleWallAreas.Add(new Vector2(u, -1));
possibleWallAreas.Add(new Vector2(u, CoinGenerator.instance.rows + 1));
}
for(int o = 0; o <= CoinGenerator.instance.rows; o++)
{
possibleWallAreas.Add(new Vector2(-1, o));
possibleWallAreas.Add(new Vector2(CoinGenerator.instance.collumns + 1, o));
}
PlaceFloor();
PlaceWall();
}
public void PlaceFloor()
{
for(int h = 0; h <= possibleFloorAreas.Count; h++)
{
GameObject theFloor = Instantiate(floor, possibleFloorAreas[h], Quaternion.identity);
tileSets.Add(theFloor);
theFloor.transform.SetParent(landParent.transform);
}
}
public void PlaceWall()
{
for (int d = 0; d <= possibleWallAreas.Count; d++)
{
GameObject theWall = Instantiate(wall, possibleWallAreas[d], Quaternion.identity);
tileSets.Add(theWall);
theWall.transform.SetParent(landParent.transform);
}
}
void DestroyLand()
{
foreach (GameObject tile in tileSets)
{
Destroy(tile);
}
}
}
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Vector2].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) FloorGenerator.PlaceFloor () (at Assets/Scripts/FloorGenerator.cs:53) FloorGenerator.DiscoverAreas () (at Assets/Scripts/FloorGenerator.cs:45) FloorGenerator.Start () (at Assets/Scripts/FloorGenerator.cs:17)
-Problem- The problem is, only the floor of the land is instantiated, the walls aren't, but the argument takes me to a part (line 53) which is the part where the floor is instantiated. The floor instantiating part is working, the wall part isn't, so I have no clue why it's taking me to the process of where the floor instantiates.
I've made the Vector 2 wall areas public in the unity editor, and when I start the game all of the vector 2 areas are found; just not used to instantiate. The wall instantiation for loop is exactly the same as the floor for loop, so I'm not sure what the problem is.
(CoinGenerator.instance.collumns/rows are just integers from another script that I'm using. They're not the problem, the problem is from instantiating with the list, not finding Vector 2's for the list)
Answer by unit_nick · Sep 30, 2017 at 04:30 AM
You are exceeding the size of the lists in your for loops. Change lines 52 & 62
//for (int h = 0; h <= possibleFloorAreas.Count; h++)
for (int h = 0; h < possibleFloorAreas.Count; h++)
//for (int d = 0; d <= possibleWallAreas.Count; d++)
for (int d = 0; d < possibleWallAreas.Count; d++)
Your answer
![](https://koobas.hobune.stream/wayback/20220612135946im_/https://answers.unity.com/themes/thub/images/avi.jpg)