Search Unity

Tilemap.SetEditorPreviewColor not working (not changing color of preview-tile)

Discussion in '2D' started by HuldaGnodima, Feb 27, 2021.

  1. HuldaGnodima

    HuldaGnodima

    Joined:
    Oct 10, 2016
    Posts:
    110
    Hi all!

    I've found very little information about this, but when I call the function
    "Tilemap.SetEditorPreviewColor" on my preview-tile it doesn't change the color of it.
    Has anyone else had this problem? If so how did you fix it?

    I'm setting the preview-tile (which is working) like below, however the color-changing-call seems to do nothing:

    Code (CSharp):
    1.      
    2. public Tilemap groundTilemap;
    3. public TileBase prevTile;
    4.  
    5. //rest of code...
    6.  
    7. //Inside function, trying to set a preview tile and then color it RED below
    8. groundTilemap.SetEditorPreviewTile(getPositiong(), prevTile);
    9. groundTilemap.SetEditorPreviewColor(getPositiong(), new Color(1, 0, 0));
    10.  
    (Documentation on the function here: https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetEditorPreviewColor.html )

    Thanks in advance!
     
  2. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    HuldaGnodima likes this.
  3. HuldaGnodima

    HuldaGnodima

    Joined:
    Oct 10, 2016
    Posts:
    110
    Thanks for this resource!

    In the link they seem to discuss colors of regular tiles however, and that I have no problem changing the color of! Changing regular tile-colors for me works just as intended (using the function Tilemap.SetColor()) :)

    What I'm having trouble with is the "preview-tiles" which have their own set of functions. So I can color my regular tiles fine but preview-tiles don't work with color-changes (despite using the assigned function). That is the odd part.

    If you know of any resources regarding problems with "preview-tile"-color changes I'd love to see them!
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Setting the color of an Editor Preview Tile follows the same rules as a normal Tile. Ensure that the Tile Flags of the Editor Preview Tile do not have LockColor on when setting the Editor Preview Color. The Editor Preview Color only works if there is an Editor Preview Tile set, and will not change the color of a set Tile.
     
  5. HuldaGnodima

    HuldaGnodima

    Joined:
    Oct 10, 2016
    Posts:
    110
    Thank you so much for your reply!

    Oh, I see. I didn't add any flags to my Preview Tiles, are they perhaps by default set to LockColor? I will try to inactivate this flag if so!

    Finding this information (that preview-tiles must not have flags that are set to "LockColor" for it to work) was a little tricky. Perhaps if it's possible/appropriate it would be nice if it was somehow included in the documentation for the function in some form? (for eg. here: https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetEditorPreviewColor.html)
    Just for the next person who might be stumped by this/have the same issue.

    Thanks again :)