Removing a Tile from a Tilemap

I’m doing a little prototype with the Unity Tilemap system (using a hex grid). I wanted a highlight effect when the user moves his mover a Tile and thought the easiest way would be to just layer another Tilemap over the ground.

So when the user moves his mouse over a Tile I simply need to:

a) Remove the TileBase from the previous Tile (if any)  
b) Set the TileBase on the new Tile  

Problem is that Tilemap doesn’t have a ClearTile or RemoveTile method. It has a ClearAllTiles() method but that does some extra things I’d like to avoid, also it might not be applicable to use in all scenarios.

What works is calling SetTile with null like so SetTile(position, null). Now the documentation says nothing about this… All it says is
Sets a tile at the given XYZ coordinates of a cell in the tile map.

So I’m wondering if the is the “correct” way to do it or if there’s something I’m missing?

Ok, so I looked around at what you’re doing. Found out SetTile(position, null) works! Get the position by adding a grid variable and doing this:

public Grid grid;
public Tilemap tilemap;

void FunctionToGetRidOfTile()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int position = grid.WorldToCell(mousePos);
tilemap.SetTile(position, null);
}

Let me go through this.
The two variables are set where grid is your Grid, and tilemap is your Tilemap. The function is to be called whenever you want to get rid of the tile. You could probably use some functions with the grid to recreate the tile somewhere else.

Hey this actually works! Now just gotta point get the tiles location instead of mouse and we are good to go!!