Unity Tilemaps and Storing Individual Tile Data

I have been learning Unity and wanted to mess around with creating a Final Fantasy Tactics / Fire Emblem like game. I figured the best way to do this was to use Unity Tilemaps and Breadth First Searching. In order for that to work though we need to store Data on each tile we placed. Here is how I did that.

Before we start if you are new to Unity Tilemaps or looking for more information about them I highly recommend checking out Brackeys video on them:

Let’s Begin. I have created a new tilemap in my project.

Unity Tilemap in Hierarchy

On Tilemap_Ground I placed down all my walkable tiles.

Walkable Tiles

Now Tilemaps are kinda funky. I found a massive amount of tutorials on creating palettes, laying down tiles, creating colliders, but nothing on how to store data per each individual tile that our Breadth First Searching is going to need. Have no fear though I figured out a solution!

First thing we are going to need to do is to create a POCO Model for our Tiles, I called mine `WorldTile` mostly because the 2d extras I brought in (see video above) has a class named `Tile` and I didn’t want to conflict names. I also added a few properties I will need later when I do Breadth First Searching.

Important don’t forget anytime we are working with tilemaps in scripts to include using UnityEngine.Tilemaps;!

WorldTile.cs POCO Model

Done with that? Good. Now lets loop through each tile we placed down in the Tilemap_Ground and set data to each one. Select Grid in your hierarchy and add a new Script. I called my script GameTiles.

Let me explain the above code a tiny bit.

I made my GameTiles an instance, that way I could easily access it elsewhere in other scripts by doing GameTiles.instance. You can see in the Awake() where I set that up.

Tilemap is also required in this script, these are the tiles we are gonna loop through in my case that tilemap is my Tilemap_Ground.

I then add a Dictionary. It has the key Vector3 which I am making our WorldTile location. The value is our WorldTile. I named this Dictionary tiles.

Now in Awake(), after setting up our instance, we call GetWorldTiles() which is where the magic begins. Here we instantiate tiles. Then we loop through each position in our tilemap. At each position we set the position we are at to localPlace. We then check if the position (localPlace) we are currently at HasTile. Not all positions we iterate through may have a tile. If it does however we are going to store it. Basically we instantiate a new WorldTile as tile and being setting data to it. After we set data we Add it to our dictionary tiles.Add(tile.WorldLocation, tile).

Finally let’s test this out! I created an empty GameObject in the Hierarchy. I then attached the following test script to it:


Success! I am able to click around and turn tiles green! We also have that data available anywhere in this scene. I really hope this helps out let me know if it does.




Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Allen Hendricks

Allen Hendricks

More from Medium

Unity Collaborate To Plastic SCM Upgrade Adventure

Unity ML-Agents 2022: Installation

Updating the textures

Installing and creating a new Unity project