Search code examples
c#unity-game-engine

Why isn't Vector3Int working to set tile?


Why isn't Tilemap.SetTile working? I am trying to set the tile but unity keeps giving errors.

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

public class till_soil : MonoBehaviour
{
    public Tile soil;
    public Tilemap tileMap;

    public Vector3Int position = new Vector3Int(0, 0);
    
    [ContextMenu("Paint")]
    void Paint()
    {
        Tilemap.SetTile(position, soil);
    }
}

I tried to use both Vector3 and Vector3Int, but I just got errors. My tiles are 16x16 pixels.


Solution

  • You're trying to use SetTile in a static way (as in; without reference to any object) by calling it directly on the Tilemap class:

    Tilemap.SetTile(position, soil);
    

    You need to use your tileMap variable that you declared earlier in the file instead, so Unity "knows" which tilemap you want to actually modify (because you can have more than one!):

    tileMap.SetTile(position, soil);
    

    This can be seen in the Unity Documentation for Tilemap.SetTile under the "Declaration" header: the method signature lacks the static keyword:

    public void SetTile(Vector3Int position, Tilemaps.TileBase tile);