- Home /
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?
For all I could read in the forums that seems to be the right way to do it. But I can't either confirm it. The documentation in those regards is really poor...
Answer by Minespeed1009 · Jun 10, 2020 at 04:59 AM
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.
Answer by Durium · Feb 26 at 07:04 PM
Hey this actually works! Now just gotta point get the tiles location instead of mouse and we are good to go!!
Your answer
Follow this Question
Related Questions
Cannot assign the tile png to the tile asset file 0 Answers
Lines between Tiles when using Tile Palette (2021) 1 Answer
3D Tile Editor? 1 Answer
tilemaps - prefab and tile issues 1 Answer
Anyone knows how to refresh tiles? 0 Answers