How to make specific Tiles of Tilemap invisible?

Hello. Im currently working on a 2d game.
Now I have made a level with the tilemap feature.
I have some spikes embedded into the ground, that should be black when initiated and only become visible when the player steps on the collider box above it. The floor and the spikes are just tilemap elements.

I would like to access the tiles in the script like they are displayed in the “Grid selection inspector”.
For example “Tile tile1 = new Tile (xPos, yPos);” and then just set its opacity upon entering / leaving the box collider.

Unfortunately I do not understand how to access the specific tile in the script. Could you tell me this please ?

I also cannot iterate through every element in a box collider and change its color ?!
Using materials is also not possible as I understand since Tiles do not have it.

Do I really have to make all these tiles gameObjects and set their opacity manually ?

Here is an Image of the “grid selection Inspector” showing the x and y position of the grid cell I want to access in the script.

I would solve this by creating two seperate tiles and depending on the game state, set the right tile.
Else you change all the tiles with spikes in your game at once.

In these cases I ask ChatGPT to come with some code examples ;-).

Here are some example codes I use:

public TileBase Building;
public TileBase Grass;

void function(){
  Vector3 mouseWorldPos = cam.ScreenToWorldPoint(Input.mousePosition);
  var pos = Ground.Tilemap.WorldToCell(mouseWorldPos);
  Tilemap.SetTile(pos, Building);
}

Hope it helps

Yes that is better. GameObjects are better for interactive objects. Because what if you want them to come back on a timer? Or also create some fx? Or momenetarily pop up then slide down? Or … any other gameplay considerations.

That’s why a GameObject plus a MonoBehaviour-derived script would be better in this situation.

Tiles are best for efficiently rendering non-interactive background visuals. Loading them up wtih all kinds of gameplay effects is not advised.

Sure, you could clear them with SetTile(pos, null) or SetColor(pos, Color.clear) but that’s a worse solution long term and is more brittle.

2 Likes