Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SetTile using a Unity Tile Palette

Discussion in 'Scripting' started by Playniax, May 8, 2018.

  1. Playniax

    Playniax

    Joined:
    Nov 30, 2016
    Posts:
    19
    Hi,

    I am a Unity beginner and I am playing with the new Tile features of Unity and I am trying to set a tile on a tilemap using SetTile.

    Now I have a gameobject that is moving on a tilemap and I wanted it to erase the tile underneath and replace it with another tile that is in my tile palette.

    Now how do I get the tile from the palette as a tile or tilebase so I can use SetTile correctly?

    Here is what I got so far where I want the null tile to be replaced with a tile from my palette.
    Code (CSharp):
    1. Vector3Int cellPosition = grid.WorldToCell(-transform.position);
    2. TileBase tile = tilemap.GetTile ( cellPosition );
    3.         if (tile)
    4.         {
    5.             tilemap.SetTile(cellPosition,null);
    6.         }
    7.  
    Thanks in advance!
     
  2. Playniax

    Playniax

    Joined:
    Nov 30, 2016
    Posts:
    19
    Ah, I found a solution, not sure if it is the best but I moved my tileset to the resources folder so I can use the Resources.Load command. Other examples are welcome here btw!