0
0
UnityHow-ToBeginner ยท 3 min read

How to Use Tilemap in Unity: Step-by-Step Guide

In Unity, use the Tilemap component inside a Grid GameObject to create 2D tile-based maps. Add tiles to the Tilemap using the Tilemap.SetTile() method or the Tile Palette window for visual editing.
๐Ÿ“

Syntax

To use a Tilemap in Unity, you first create a Grid GameObject, then add a Tilemap component as a child. You can set tiles programmatically using Tilemap.SetTile(Vector3Int position, TileBase tile).

  • Grid: Parent object that defines the layout for tiles.
  • Tilemap: Component that holds and displays tiles.
  • TileBase: The tile asset to place on the Tilemap.
  • Vector3Int position: The grid cell coordinates where the tile is placed.
csharp
using UnityEngine;
using UnityEngine.Tilemaps;

public class TilemapExample : MonoBehaviour
{
    public Tilemap tilemap;
    public TileBase tile;

    void Start()
    {
        Vector3Int tilePosition = new Vector3Int(0, 0, 0);
        tilemap.SetTile(tilePosition, tile);
    }
}
๐Ÿ’ป

Example

This example shows how to place a single tile at position (0,0) on a Tilemap using a script. It assumes you have a Tilemap and a Tile asset assigned in the Unity Editor.

csharp
using UnityEngine;
using UnityEngine.Tilemaps;

public class SimpleTilemap : MonoBehaviour
{
    public Tilemap tilemap;
    public TileBase tile;

    void Start()
    {
        // Place tile at grid position (0, 0)
        Vector3Int position = new Vector3Int(0, 0, 0);
        tilemap.SetTile(position, tile);
    }
}
Output
A tile appears at the center of the Tilemap grid in the game scene.
โš ๏ธ

Common Pitfalls

  • Forgetting to add a Grid parent GameObject before adding a Tilemap.
  • Not assigning the Tilemap and TileBase references in the script inspector.
  • Using incorrect Vector3Int coordinates outside the Tilemap bounds.
  • Trying to set tiles before the Tilemap component is initialized.

Always check that your Tilemap and Tile assets are properly linked and that your coordinates are valid.

csharp
/* Wrong: Missing Tilemap reference */
public Tilemap tilemap;
public TileBase tile;

void Start()
{
    tilemap.SetTile(new Vector3Int(0,0,0), tile); // NullReferenceException if tilemap is null
}

/* Right: Assign tilemap in inspector or find it in code */
void Start()
{
    if(tilemap == null)
        tilemap = GetComponent<Tilemap>();
    tilemap.SetTile(new Vector3Int(0,0,0), tile);
}
๐Ÿ“Š

Quick Reference

  • Grid: Parent object for Tilemaps.
  • Tilemap: Component to hold tiles.
  • TileBase: Tile asset type.
  • SetTile(position, tile): Place a tile at a grid position.
  • ClearAllTiles(): Remove all tiles from the Tilemap.
โœ…

Key Takeaways

Create a Grid GameObject and add a Tilemap component to use tilemaps in Unity.
Use Tilemap.SetTile() with Vector3Int coordinates to place tiles programmatically.
Assign Tilemap and TileBase references in the inspector to avoid null errors.
Use the Tile Palette window for easy visual tile placement during level design.
Check tile coordinates and Tilemap initialization to prevent common mistakes.