How can I add script to a single tile ?

Hi, I am developing a 2D platformer game. The map is created by tilemap. But I don’t know how to add scripts to a single tile in a tilemap.

Like the tiles in the picture, I put trap tiles and platform tiles in a tilemap gameobject. How can I add script to trap tiles so that I can take damage to player whenever player touch the trap tiles?
If I have different types of trap tile, should I put them in different tilemap?

@Frankorz I searched a very long time for the answer to this and I finally came up with a solution.

The first thing you will want to do is download the 2D Extras folder from Unity’s GitHub found at:

Click on “Clone or download” and choose “Download Zip”
Once it has downloaded, unzip it and drag the contents into “Assets” in your Unity project.

In Unity, in your project folder, right click and choose Create < Prefab Brush and name it whatever you choose. If you don’t see it in the create menu, try putting the cursor at the very top of the list and see if it scrolls. The “2D Extras” adds so much to the menu that it is larger than the screen and you may have to scroll to find it.

Now you need to create a prefab which is going to act as your tile. A tile is essentially just an image and a collider, so create an empty gameobject in the hierarchy and add these components in the inspector: Sprite Renderer & Box Collider 2D. Change the sprite to whatever you want the tile to be. Now you can add a script to the prefab. I made a very simple script just for test purposes:

void OnMouseDown(){

However, once you know this is working you can make the script anything you want it to be. Now, drag the gameobject into the project folder to make it into a prefab.

Now select your Prefab Brush in your Tile Palette and drag your Prefab into the "Prefabs"in the inspector of the brush. This is an array and you can add multiple prefabs if you so choose; however, this will choose one of the prefabs at random, according to the “Perlin Scale” slider which you can change. You will most likely need a dedicated brush for each prefab.

To use the brush, select it from within the Tile Palette and use it as you would the default brush.

This is the solution that I was able to come up with. If anyone finds a better method, please fill us in, as this method does have its downsides, but it does work. The references I used to help come up with this are:

TILEMAPS in Unity - YouTube

I would of just used 2d colliders and attached a script to them and use them for OnCollisionEnter2D methods as the brush tools can go outdated and cause problems in the future updates of unity.

I had similar issues. I used one tilemap for the terrain (and ruleTile in 2d extras is very useful for this). Then for single items, including traps, coins, chests, I didn’t use tilemap at all. Each one is an individual gameobject with spriteRenderer, animator, box collider 2d, and custom script components.
To create one item from scratch, I dragged its sprites into the scene to make a new animation. And an animated object will also be created in the scene. You can modify the details in the animator controller (like the speed of the animation). I also use animation events to activate/deactivate traps.