- Home /
SetColor() In Tilemaps Isn't Working.
Hey everyone, I was hoping that someone can help me with this problem I am having with Tilemap.SetColor(). For me, this function isn't working to set the color of the tiles. I have read through some things saying to set the tile flags to none, and that hasn't worked. I have also tried to use the Tilemap.RemoveTileFlags() function as you can see in the commented out code below.
public void calcLighting(int x, int y, int lightingDist, int increments)
{
//Check if the block that we are checking is empty.
if (!isInWorld(x, y)) return;
if (worldBlocks[y, x] == null) return;
Vector3Int thisPos = new Vector3Int(x, y, 0);
//Then check all the blocks within a certain radius.
for (int d = 0; d < 360; d += increments)
{
for (int dist = 0; dist < lightingDist; dist++)
{
int newX = Mathf.RoundToInt(Mathf.Cos(d) * dist) + x;
int newY = Mathf.RoundToInt(Mathf.Sin(d) * dist) + y;
if (!isInWorld(newX, newY)) continue;
if (worldBlocks[newY, newX] == null)
{
int newLightLev = 0;//1 - (dist / lightingDist);
Color lightingColor = new Color(newLightLev, newLightLev, newLightLev);
savedChunks[getChunk(new Vector2Int(x, 0))].chunkTilemap.SetTileFlags(thisPos, TileFlags.None);
//savedChunks[getChunk(new Vector2Int(x, 0))].chunkTilemap.RemoveTileFlags(new Vector3Int(x, y, 0), TileFlags.LockColor);
savedChunks[getChunk(new Vector2Int(x, 0))].chunkTilemap.SetColor(thisPos, lightingColor);
savedChunks[getChunk(new Vector2Int(x, 0))].chunkTilemap.RefreshTile(thisPos);
if (x == 0) Debug.Log("Column" + y + " light level: " + savedChunks[getChunk(new Vector2Int(x, 0))].chunkTilemap.GetColor(thisPos).r + " wanted: " + lightingColor.r);
}
}
}
}
I would have added the whole script for you all but it is around 700 lines long and none wants to read through all of that (let me know if I should post the whole script). This is a segment of code from a 2D procedural world generation script that I made to generate something like this:
Back to the code, the function "isInWorld" takes 2 coordinates (an x and a y) and checks if that position is within the boundaries of the generated world.
The array "worldBlocks" stores variables of a custom class that just stores some data for each place in the generated world.
The array "savedChunks" is of a custom class that I made that just stores a Tilemap (among other things) that can be loaded in or out for game optimization.
If a cell in "worldBlocks" is equal to "null", that just means that it is an empty air block on the map.
Anyways the Debug at the bottom of the code returns "Column [yes, I understand that I should have labeled it row instead of column] (y coordinate) light level: 1 wanted [color]: 0"
I honestly have no idea why this isn't working, and have spent around 7-10 hours just making no progress. I would be SO grateful if anyone could help! Thank you!
Edit: The for loops that the debug is embedded in are used to basically run through all (or most) of the blocks in a circular area around the specified "x" and "y" coordinates. The radius of this area is specified by the "lightingDist" variable.