Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Frankorz · May 25, 2018 at 03:46 AM · tilemaptiles

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.
alt text

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?

tim截图20180525133926.png (30.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frankorz · Jun 13, 2018 at 12:31 AM 1
Share

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.

avatar image ZanyT Frankorz · Jun 13, 2018 at 12:48 AM 0
Share

@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.

avatar image Frankorz ZanyT · Jun 13, 2018 at 02:20 AM 0
Share

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.

alt text

Here is howwe deal with tilemap:

alt text alt text

Thanks for your detail solution again.

tilemap.png (43.0 kB)
trap.png (35.7 kB)
avatar image robwill · Nov 03, 2018 at 03:52 PM 0
Share

This was very useful for my school project dude. Thanks so much! +1

avatar image lojzl · Feb 24, 2021 at 05:05 PM 0
Share

thx i always use this method

avatar image zorphis2 · Sep 19, 2021 at 05:18 AM 0
Share

to clone the version specific branch use this command: git clone https://github.com/Unity-Technologies/2d-extras.git --branch --single-branch [folder]

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges