I’ve got an issue with assigning keys to new dictionary positions using a loop. this is my code:
public void GenerateMapTiles()
{
for (int x = 0; x < 10; x++)
{ for (int z = 0; z < 10; z++)
{ TileTypes2 tt = tileType[tile[x, z]];
GameObject go = Instantiate(tt.tileVisualPrefab, new Vector3(x, 0, z), Quaternion.identity);
OnTile ot = go.GetComponent<OnTile>();
ot.tileX = x;
ot.tileZ = z;
trueTiles.Add(new int[x, z], ot);
//Debug.Log(trueTiles.Count);
}
}
}
I also have the Dictionary created like this:
public Dictionary<int[,], OnTile> trueTiles = new Dictionary<int[,], OnTile>();
But when I call for an ‘OnTile’ script using a key such as [1,1] it just throws an error and says the given key was not in the dictionary.
public void TEST()
{
for(int x = 0; x < 10; x++)
{
for (int z = 0; z< 10; z++)
{
Debug.Log(trueTiles.ContainsKey(new int[x, z]));
}
}
}
but when I called that function, it just returns false, for every single item.
and, when I do trueTiles.keys;
it returns a list of keys but in the console it shows each key as ‘Int32[,]’, so I’ve come to the conclusion that the loop i have is not setting the keys for some reason.
I’m new to Coding and I don’t understand what I’m doing wrong here and I would appreciate it if somebody could tell me how to fix this. or if somebody could tell me a better way to do this?
//My goal with this script is to create a way to access an ‘OnTile’ script by using its respective position in the map in an x,z format like so:
if( trueTiles[new int[5, 4]].isOccupied == true)
{
//do something
}