0
\$\begingroup\$

I created a simple prototype/script which has to draw a predefined tile on an existing tilemap (and highlight the hovered tile).

But for some reason, even though it adds the tile to the correct place, it does not render it in game mode. The tile is only visible in edit mode. Why is that?

I am using Unity version 2022.3 with the Universal Render Pipeline (URP).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class TilePlacer : MonoBehaviour
{
    public Tilemap tilemap;
    public TileBase tileToPlace;

    public Color highlightColor = new Color(1, 1, 1, 0.5f);
    private Vector3Int lastHighlightedCell = Vector3Int.one * int.MaxValue;

    private void Update()
    {
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3Int currentCell = tilemap.WorldToCell(worldPos);

        if (lastHighlightedCell != currentCell)
        {
            tilemap.SetColor(lastHighlightedCell, Color.white);
            tilemap.SetColor(currentCell, highlightColor);
            lastHighlightedCell = currentCell;
        }

        if (Input.GetMouseButtonDown(0) && tilemap.GetTile(currentCell) == null)
        {
            PlaceTileUnderMouse(currentCell);
        }
    }

    private void PlaceTileUnderMouse(Vector3Int currentCell)
    {
        tilemap.SetTile(currentCell, tileToPlace);
        tilemap.RefreshTile(currentCell);
    }
}
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .