How to SetTile inside GetTileData ?

Hey guys, I am trying to create a Tile using the new Tilemap system, that when I paint this tile automatically paints other 2 tiles for me.

Think about a tree, I paint the bottom part of the tree and the Tile paints the trunk and the top for me automatically.

I am trying to do this inside the GetTileData method, but everytime I call tilemap.SetTile inside this method and drag the Tile to the pallete Unity crashes.

Anyone knows if this is a bug or if I can’t do this ?


The code I am using is:

public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    {
        Tilemap tilemapInstance = tilemap.GetComponent<Tilemap>();

        tilemapInstance.SetTile(new Vector3Int(position.x, position.y - 1, position.z), trunkTile);
        tilemapInstance.SetTile(new Vector3Int(position.x, position.y - 2, position.z), topTile);

        base.GetTileData(position, tilemap, ref tileData);
    }

I will reply my own question, since I got it working.
I created a new kind of Tile that “propagates” other configured tiles based in offsets of the main tile position. Using this tile, allows you to automatically create trees with a single click and the trunk, leaves and roots are painted in different tilemaps.

You can find the script attached on this reply. I hope it helps someone.

EDIT: the attachment are .cs files just rename the extensions after download :slight_smile: