- Home /
How can I change a tile color in Unity by using C#.,
How can I change a tile or tilebase color in unity.. There's no GetComponent function in TileBase and I was wondering if there's a way. I wanted to have flashing effects on my tiles to make the game more lively; but I dont know how to do that if I don't know how to change the tile color in Unity. Please help if you can..,
Isn't Tilemap.SetColor
what you are looking for?
https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetColor.html
Answer by George_Coleman · Nov 06, 2018 at 02:34 AM
To change a tile's colour in code you have to access it via a position on a tilemap and flag it to indicate it can change colour.
This is working for me:
/// <summary>
/// Set the colour of a tile.
/// </summary>
/// <param name="colour">The desired colour.</param>
/// <param name="position">The position of the tile.</param>
/// <param name="tilemap">The tilemap the tile belongs to.</param>
private void SetTileColour(Color colour, Vector3Int position, Tilemap tilemap)
{
// Flag the tile, inidicating that it can change colour.
// By default it's set to "Lock Colour".
tilemap.SetTileFlags(position, TileFlags.None);
// Set the colour.
tilemap.SetColor(position, colour);
}
Note that the position passed into SetTileFlags and SetColour uses the tilemap's local positon to access the tile. So if your tilemap's not positioned at the origin, you'll need to adjust accordingly.
I'm trying to do that, but the SetTileFlags make the tiles become invisible
Haha, had the same problem. When using a predefined static colour like Color.blue and no variable colour it was working. Set the alpha value of your colour variable value to 255. Then you might see the coloured tile as expected.
when i use this it only sets it for one tile and i want it to be the tile the player is standing on, how can i fix this?
Answer by FernandoHC · Aug 27, 2018 at 03:02 PM
In order to change a tile sprite/color you have to access it through the tilemap, specifically the type "Tile", Tilebase will not have such features.
More info at https://docs.unity3d.com/Manual/Tilemap-ScriptableTiles-Tile.html
Your answer
Follow this Question
Related Questions
Make the cube have the same color as the background every 3 seconds 0 Answers
Instantiating multiple sprites and assigning different colors for each 1 Answer
How to create shared color channels 1 Answer
How to change the color of an image every two seconds 1 Answer
How to change colors in turn? 1 Answer