- Home /
Question by
BeanBugDev · Dec 08, 2019 at 01:16 PM ·
tilemaptiles
GetTile always returning null
When I loop through my tilemap's tiles, I always get null even though most tiles are assigned. Note that frontLayer and backLayer are not null.
I can't figure out why this is. I've tried with both frontLayer.GetTile(frontLayer.WorldToCell(new Vector3Int(x, y, 0)))?.name
and frontLayer.GetTile(new Vector3Int(x, y, 0))?.name
The code:
Boolean[,] caveMap = new Boolean[width, depth];
Tilemap frontLayer = GameObject.Find("BlocksFront").GetComponent<Tilemap>();
Tilemap backLayer = GameObject.Find("BlocksBack").GetComponent<Tilemap>();
frontLayer.CompressBounds();
backLayer.CompressBounds();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < depth; y++)
{
if (backLayer.GetTile(new Vector3Int(x, y, 0)) != null)
Debug.Log(frontLayer.GetTile(new Vector3Int(x, y, 0))?.name + ":" + backLayer.GetTile(new Vector3Int(x, y, 0))?.name);
//checks for air on front layer (cave or air) AND stone in back layer (stone or cave)
if (frontLayer.GetTile(new Vector3Int(x, y, 0))?.name == "air" && backLayer.GetTile(new Vector3Int(x, y, 0))?.name == "stone")
{
caveMap[x, y] = true;
}
else
{
caveMap[x, y] = false;
}
}
}
annotation-2019-12-08-061444.png
(210.1 kB)
Comment