Search Unity

Tilemap.SetColor problem

Discussion in '2D' started by Tisamous, Nov 20, 2017.

Thread Status:
Not open for further replies.
  1. Tisamous

    Tisamous

    Joined:
    Feb 23, 2017
    Posts:
    2
    Hi guys,

    I'm asking for a help with Tilemap.setColor. I can't make it working. I have no idea what's wrong. For example, I want to change from white to red but, as you can see from the debuging screenshot, it's not happening. The color stays white; r = 1, g = 1, b = 1.
    I appreciate any help.



    Here is the code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5.  
    6. public class TermalTileMap : MonoBehaviour {
    7.  
    8.    [SerializeField]
    9.    private TileBase[] tileBase;
    10.    private Tilemap tileMap;
    11.  
    12.    private float[,] blockTempDataMap = new float[250, 150];
    13.  
    14. void Start ()
    15.    {
    16.        tileMap = GameObject.Find("TermalTileMap").GetComponent("Tilemap") as Tilemap;
    17.  
    18.        for (int y = 0; y < blockTempDataMap.GetLength(0); y++)
    19.        {
    20.            for (int x = 0; x < blockTempDataMap.GetLength(1); x++)
    21.            {
    22.                tileMap.SetTile(new Vector3Int(y, x, 0), tileBase[0]);
    23.                tileMap.SetColor(new Vector3Int(y, x, 0), Color.red);
    24.            }
    25.        }
    26. }
     
  2. Tisamous

    Tisamous

    Joined:
    Feb 23, 2017
    Posts:
    2
    Solved. In default, the color is locked. Setting the TileFlags to none helped.
     
    makuk369 and Raziid like this.
  3. kimjooho

    kimjooho

    Joined:
    Jan 10, 2015
    Posts:
    4
    would you post setFlags code, please? I tried but not works.

    Code (CSharp):
    1. public void DrawTile(Coord coord, MapLayer layer, string tileName, Color? color = null)
    2.     {
    3.         if (TileMapDic.ContainsKey(layer))
    4.         {
    5.             TileMapDic[layer].SetTile(new Vector3Int(coord.X, coord.Y, 0), TileDB.GetTile(tileName));
    6.  
    7.             //if (color !=null){
    8.             TileMapDic[layer].SetTileFlags(new Vector3Int(coord.X, coord.Y, 0), TileFlags.None);
    9.  
    10.             TileMapDic[layer].SetColor(new Vector3Int(coord.X, coord.Y, 0), Color.blue);
    11.  
    12.  
    13.             TileMapDic[layer].RefreshTile(new Vector3Int(coord.X, coord.Y, 0));
    14.         }
    15.         else
    16.         {
    17.             throw new Exception("there's no layer : " + layer);
    18.         }
    19.     }
     
  4. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    244
    Hey did you end up working it out? I'm having the same trouble.
    Tried what was suggested above too but no result.

    Used this code

    Code (CSharp):
    1. tileMap.SetTileFlags(location, TileFlags.None);
    2. tileMap.SetColor(location, Color.black);
     
  5. SlapworthFH

    SlapworthFH

    Joined:
    Jun 16, 2017
    Posts:
    3
    I'm having the same problem here. I can't get SetColor to work, even after setting tileflags to none. Any idea what I'm missing?

    Thanks - SlapworthFH

    UPDATE: I figured it out... or rather, I realized I was having the problem described and solved here: https://forum.unity.com/threads/color-of-a-tile-tilemaps-unity-2017-2.496469/#post-3371739

    I had to select the tile in the tile palette, AND set the inspector into Debug mode, so I could stop the tile from locking color.
     
    Last edited: Jul 8, 2018
    Brogan89 likes this.
  6. Salmakis

    Salmakis

    Joined:
    May 14, 2018
    Posts:
    15
    how to solve this if i dont use a palette?
    i just create the tiles by code
     
  7. HuldaGnodima

    HuldaGnodima

    Joined:
    Oct 10, 2016
    Posts:
    110
    Did you ever figure it out? I'm in the same boat right now.

    Edit: I figured it out and will write here if someone else finds this thread!
    I thought I couldn't get it to work by using the "tilemap.SetTileFlags()" function via code, but it seems you have to call the "tilemap.SetTileFlags()" AFTER setting down the tile (not before), and then call "SetColor" last (I had called SetTileFlags before SetTile).

    It seems this is because SetTile can override whatever flags are on the coordinate already, so SetTileFlags should in that case be called afterwards. I got it to work like this:

    Code (CSharp):
    1.             tilemap.SetTile(coordinate, myTile);
    2.             tilemap.SetTileFlags(coordinate, TileFlags.None);
    3.             tilemap.SetColor(coordinate, myTileColor);
    Hope this helps someone!
     
    Last edited: Mar 31, 2021
    Salmakis and ChiuanWei like this.
  8. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    You could derive a new class of tile from TileBase which by default has permissive TileFlags setting. That would mean you don't need to SetTileFlags at all.
     
    S1lencely likes this.
  9. iGoA

    iGoA

    Joined:
    Aug 23, 2020
    Posts:
    22
    HuldaGnodima likes this.
Thread Status:
Not open for further replies.