I have four tilemaps in my 2D URP project:
Ground, Walls, Path, Selection
They have their own ordering (0, 1, 2, 3 respectively).
Now when a user mouse overs a grid, I want to draw a highlight tile on the Selection tilemap.
I wrote this code:
public class GridController : MonoBehaviour
{
[SerializeField] Tilemap selectionHighlights;
[SerializeField] Tile selectionTile;
private Grid grid;
// Start is called before the first frame update
void Start()
{
grid = GetComponent<Grid>();
}
Vector3Int GetMousePosition()
{
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
return grid.WorldToCell(mouseWorldPos);
}
// Update is called once per frame
void Update()
{
var mousePos = GetMousePosition();
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse position over " + mousePos.x + "/" + mousePos.y);
selectionHighlights.SetTile(mousePos, selectionTile);
}
}
}
When I run the game, nothing happens. I see the log message, but not the tile being created.
If I PAUSE the game, the tiles I created are visible. If I continue playing the game, the tiles disappear.
I’m not sure what I’m doing wrong, anyone have a clue?