Tilemap saved as a prefab breaks

Hi,

I have a problem with Tilemaps updated through a code, which completely break, after I save them as prefab. This happens after I import layouts of my levels from a text file.

What I do is:

  1. load a prefab for an empty planet (level). It consists of a parent object with Grid Component and two children nodes with Tilemap and Tilemap Renderer components. Generally I use the same setup, when I create my levels manually and it works.
  2. I iterate over the rows and columns of the level and load tiles into memory as required - a dictionary that maps tile name to the tile objects
  3. for every cell, I instantiate a Tile (it can be a custom GameTile or a RuleTile) and set them in appropriate Tilemap using SetTile() method.
  4. The result is somewhat promising, but only partially - no RuleTiles are working, even though I run RefreshAllTiles() on both tilemaps. I also tried calling it on every Tile individually, but no difference.
  5. But the real problem I have is that once this is saved as a prefab, all the tiles turn into plain colour squares with a random red/pink tint.

If I paint over manually, the RuleTiles painted appear correct - see another screengrab. The manually placed tile save correctly into prefab - it’s just the scripted ones that are broken.

Do you have any idea what I’m missing? Is there anything wrong with this process? (loading a level prefab, modifying it and saving as a new prefab).

I can provide listing of the code, if it helps.

OK, nevermind - found my problem.

Basically, I should NOT instantiate the tiles - just link them to the loaded prefabs. That’s it.

I needed to post this on the forum, to get myself enlightened! :smiley: