I’m trying to use a list to store values for a tile map using the following class:
class TerrainInfo {
public int terrainTileID;
public GameObject terrainGameObject;
public TerrainInfo(int tileID, GameObject gameObject) {
terrainTileID = tileID;
terrainGameObject = gameObject;
}
}
From what I can tell, this is set up correctly. Now, after making sure the list is set up correctly using…
List<TerrainInfo> terrainArray = new List<TerrainInfo>();
…the next thing I do is fill this list with 0/null values just to make sure there are enough spaces in the array to store the full grid of tiles.
//Create list.
for (int x = 0; x < mapWidth; x++) {
for (int z = 0; z < mapHeight; z++) {
terrainArray.Add(new TerrainInfo(0, null));
}
}
No errors up to this point, so I’m pretty sure that’s all correct. The final bit of the code is to create the boundary tiles around the edge of the map, so I use the following code (with some removed since it’s not relevant):
//Create bondary objects.
for (int x = 0; x < mapWidth; x++) {
for (int z = 0; z < mapHeight; z++) {
if (x == 0 || x == mapWidth - 1 || z == 0 || z == mapHeight - 1) {
CreateTerrainGameObject(x, z, 1);
}
}
}
//Create Terrain Object
public void CreateTerrainGameObject(int x, int z, int tileID) {
terrainArray[arrayPosition] = (new TerrainInfo(1, Instantiate(parentTile, new Vector3(x * tileSpacing, 0, z * tileSpacing), Quaternion.identity) as GameObject));
terrainArray[arrayPosition].terrainGameObject.renderer.material.color = Color.red;
}
The “.renderer.material.color” line is just to test whether or not I can make changes to the object that was just created using a reference to its spot in the list. This is the line where the error is appearing, but I can’t figure out why. If there’s any info that left out or any clarification that needs to be made please let me know. Thanks in advance for the help!