• 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 /
avatar image
0
Question by theas00 · Jul 21, 2020 at 01:51 PM · colliderrigidbody2dcollider2doncollisionentercollision2d

HOW TO CALLBACK COLLIDER WITH NAME?

hi, sorry for my bad English, I’m trying to make a platform game 2d, the player has two objects. The parent object contains two Colliders2d (box, circle), but I don’t know how to recall the Collider by name on "onCollisionEnter" please help me!! thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tocu-adrian · Jul 21, 2020 at 02:30 PM

Hi, If i understand, you want the collider name; Try this:

 private void OnCollisionEnter2D(Collision2D collision)
 {
     var collider = collision.GetContact(0).collider;
     var otherCollider = collision.GetContact(0).otherCollider;
 }

Tell me if you have any problems.

Comment
Add comment · Show 5 · 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 theas00 · Jul 21, 2020 at 08:51 PM 0
Share

thanks, the code works, but the collision is detected in both Colliders even if the other object touches only one...

avatar image tocu-adrian theas00 · Jul 23, 2020 at 12:25 PM 0
Share

Do you want something like this: when an object collide with the box collider then he must execute an action and when it collide with the circle it must execute another action ?

For this you can do something like this :

     private void OnCollisionEnter2D(Collision2D collision)
     {
         var collisionWith = collision.GetContact(0).otherCollider;
         
         if(collisionWith.name == "box")
         {
             //do box behaviour
         }
         else if (collisionWith.name == "circle")
         {
             //do circle behaviour
         }
     }

Keep in mind that in "collisionWith.name" is stored the name of the gameobject that contain one of your collider. This is the case where you have two gameobjects as child of your parent, each of them have an collider.

But if you have one parent with two colliders. And you want to detect which collider was hit, then do this:

     private void OnCollisionEnter2D(Collision2D collision)
     {
         var collisionWith = collision.GetContact(0).otherCollider;
 
         if(collisionWith.GetType() == typeof(BoxCollider2D))
         {
             Debug.Log("Box Collider");
         }
         else if (collisionWith.GetType() == typeof(CircleCollider2D))
         {
             Debug.Log("Circle Collider");
         }
     }
avatar image theas00 tocu-adrian · Jul 23, 2020 at 02:35 PM 0
Share

thank you so much for your availability, problem solved!!

Show more comments
avatar image
0

Answer by gjf · Jul 21, 2020 at 01:51 PM

You mean OnCollisionEnter(), right?

https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Comment
Add comment · Show 1 · 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 theas00 · Jul 21, 2020 at 02:05 PM 0
Share

hi, I tried this code, but don't work....

if (GetComponent().GetType() == typeof(BoxCollider2D)) { Debug.Log("Box") } if (GetComponent().GetType() == typeof(CircleCollider2D)) { Debug.Log("Circle") }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

180 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 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

Workarounds for Higher Control When Using Physics2D? 0 Answers

How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer

How to set collision for an object with specific collider size? 3 Answers

Player jump only once 1 Answer

Disable click action when collision 1 Answer


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