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?
Answer by ZanyT · Jun 12, 2018 at 11:43 PM
@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: https://github.com/Unity-Technologies/2d-extras 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(){ Debug.Log("Hello"); }
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: https://www.youtube.com/watch?v=UqhK6GpCgrM https://www.youtube.com/watch?v=ryISV_nH8qw
I just separate the whole tilemap into three tilemap: 1. decoTilemap (flowers, grass etc) 2. platformTilemap (add Tilemap collider) 3. TrapTilemap (add collider and add Trap script to detect onCollsionEnter2D) $$anonymous$$aybe it is not a elegant solution.
@Frankorz I edited my answer to provide a solution, I could be misjudging your question, but I thought you needed a script on your tile. $$anonymous$$aybe for your specific problem a script wasn't necessary, but if you do in the future, or for others that do, I hope my answer helps.
Thanks for your reply, I get what you mean, which is regard the gameobject as a tile, then draw them on tilemap directly. In my situation, it is more convenient to separate trap tilemap away, then adding one trap script on trap tilemap is enough.(one script for all same traps) In the first video you provided, they also separate the tilemap into different functional tilemap with different usages.
Here is howwe deal with tilemap:
Thanks for your detail solution again.
This was very useful for my school project dude. Thanks so much! +1
to clone the version specific branch use this command: git clone https://github.com/Unity-Technologies/2d-extras.git --branch --single-branch [folder]
Answer by Sazails · Nov 03, 2018 at 04:02 PM
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.
Answer by YLIU146 · Sep 10, 2021 at 11:38 AM
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.
Your answer
Follow this Question
Related Questions
How to make a tilemap? 0 Answers
How to destroy a tile on collision in several parts 0 Answers
Id need some help with Navmesh and Tiles 1 Answer
How To Get Tile Cell Position 0 Answers
Fix tilemap cell size 0 Answers