- Home /
Accessing a nested list of gameobjects not working quite right
Hi I am building a grid of tiles using the following code.
Public GameObject tilePrefab;
int mapLength = 10;
int tileLength = 10;
List<List<Tile>> map = new List<List<Tile>>();
Awake()
{
MakeMap();
ShowCenter();
}
void MakeMap()
{
List<Tile> row = new List<Tile>();
for(int x = 0; x < mapLength;x++)
{
for(int z = 0; z < mapLength;z++)
{
Tile t = ((GameObject)Instantiate(tilePrefab,new Vector3(x * tileLength, 0, z * tileLength),new Quaternion.Euler(new Vector3())).GetComponent<Tile>();
t.mapPosition = new Vector2(x,z);
row.Add(t);
}
map.Add(row);
}
}
void ShowCenter()
{
//maplength will always be an odd number
int center = (int)Mathf.Round(mapLength / 2);
Tile c = map[(int)center][(int)center];
c.transform.renderer.material.color = Color.red;
}
The issue I am having is the ShowCenter method, I will only have the first column affected and show the tile at the correct location in one dimension only not both the x and z dimensions.
I hope I am explaining this well enough. below is the bare bones of my tile class which is attached to my Tile Prefab GameObject.
Any help is appreciated.
public class Tile
{
public Vector2 mapPosition = Vector2.zero;
}
Well never $$anonymous$$d I fixed this after reviewing my $$anonymous$$ake$$anonymous$$ap method, I should have initialize the row inside of the x loop. Problem solved.
Answer by whydoidoit · Apr 03, 2014 at 06:46 PM
You need to put the row initialiser inside the loop:
void MakeMap()
{
// Mov this: List<Tile> row = new List<Tile>();
for(int x = 0; x < mapLength;x++)
{
List<Tile> row = new List<Tile>(); //To here
Your answer
Follow this Question
Related Questions
Find a path with specific steps (moves) on a grid 0 Answers
How can I attach a script to all of the tiles inside of my grid? 2 Answers
How to create a rotating tiles with walls on edges of tiles? 0 Answers
ai pathfinding project getting started scene problems 1 Answer
Enemy Movement AI in top-down, 2D, tile & turn based game 1 Answer