Search Unity

Tile Color

Discussion in '2D' started by B296, Oct 10, 2019.

  1. B296

    B296

    Joined:
    Oct 10, 2019
    Posts:
    2
    Hey all,

    I'm trying to change the color of select tiles using their X and Y coordinates but everything I've tried doesn't make a difference. I've tried removing any flags like 'LockColor' from the tiles before trying to use .SetColor() and also tried refreshing the tile aftwards with no success.

    Here's the small function I'm running to test to color changing. 'tile' is being populated with Tiles elsewhere before this is called.

    Code (CSharp):
    1. public Tilemap worldTilemap;
    2.  
    3. void ColorTiles()
    4.     {
    5.  
    6.         for (int x = 0; x < worldWidth; x += 1)
    7.         {
    8.             for (int y = 0; y < worldHeight; y += 1)
    9.             {
    10.                 worldTilemap.SetTile(new Vector3Int(x, y, 0), tile[0]);
    11.                 worldTilemap.SetTileFlags(new Vector3Int(x, y, 0), TileFlags.None);
    12.                 worldTilemap.SetColor(new Vector3Int(x, y, 0), Color.red);
    13.                 worldTilemap.RefreshTile(new Vector3Int(x, y, 0));
    14.             }
    15.         }
    16.     }
    There were also some similar threads that said about 'unlocking' the grid, but I couldn't find the option so I assumed it had been updated and changed.

    Another possibility I could use help with is changing the color of a tile's resource file. I'm able to manually change the color of the tile via the inspector but I'm not sure how to do it via script.

    Thanks.
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    325
    This looks correct to me for changing the color of the Tile to red.

    I would suggest not refreshing the location after you have set your properties for a Tile on the Tilemap as this would retrieve the information from the Tile Asset once more and reset all your changes, which may include the TileFlags and the color depending on how the Tile Asset is set up.

    This is possible if you have the reference to the Tile Asset. Assuming that the Tile is of the class UnityEngine.Tilemaps.Tile, you can set the color with
    tile.color = Color.red
    . If the Tile Asset has the
    TileFlags TileFlags.LockColor
    set, when you set this Tile Asset to the Tilemap, the Tile placed on the Tilemap will have the color red.
     
    B296 likes this.
  3. B296

    B296

    Joined:
    Oct 10, 2019
    Posts:
    2
    I never managed to get SetColor working but I did manage to change the color of tiles using
    tile.color = Color.red
    .
    Thanks for the help!