Label each cell in a grid

I have a tile-based scene that is 22 cells wide by 13 cells tall. I need to place some dynamic debug text in each cell that tells me unique information about that cell (let’s say a variable that displays the number of widgets currently in that cell). Creating 286 text Game Objects in the Canvas by hand obviously doesn’t make sense. How can I programmatically add a label to display the widget count in each cell?

Hi there!

Are you generating the grid using code or are you placing all of the tiles yourself? If you are doing it with code, as soon as you instantiate the tile, you can grab the amount of “widgets” on it, create a child text object on it (still in code, using a prefab) set the text, and let it do its stuff. Here’s a link to a tutorial involving this (more than perhaps what you want, but it has a section about something like what you’re doing).

If you are placing the tiles yourself, you could still do something similar. But instead maybe you would add a separate script to each tile that does this. Adds the child text object and so on. Keep in mind if this is for debugging in the editor, if you’re not already using this, you can use [ExecuteInEditMode] to have some of your code run in the editor.

Life got in the way of this project but I’m finally back. Thanks for the tutorial @Stratosome. I was able to follow it enough to get all the grid cells labeled using prefabs. But that is done on Awake and the numbers in that tutorial don’t change, they are just labels for the position of the tiles and those tiles don’t move. Am I still able to use this method to display numbers that change over time?