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:
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.
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
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.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.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.
This is how the level looks like in the scene after importing.
And this is how it's supposed to look like with the walls and floor RuleTiles working.
Answer by JuliuszK · Nov 21, 2021 at 04:20 PM
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! :D