Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Unessential · Aug 01, 2016 at 07:25 PM · javascriptcollisionrigidbody2dcollision detectionboxcollider2d

OnCollisionEnter2D not getting called

I have two objects. both have Box Collider 2D and Rigidbody 2D with these attributes (they are the same):

Box collider:
Material: none ,
IsTrigger: checked ,
Used By Effector: Unchecked,
Offset X:0 , Y:0
Size: x:1.961539 y:2.538462

Rigidbody: isKinematic : checked (i've tried all combinations of checked and unchecked between the two objects)
Interpolate :none
sleeping mode: Start Awake
Collision Detection: continuous (i've tried all combinations of discrete and continuous between the two objects)

On one of the objects I have this code:

 function OnCollisionEnter2D(col : Collision2D) {
     
    Debug.Log("collide");
    
    if(col.collider.tag == "Objecttag"){
        Debug.Log("collide");
    }
 }


However no matter what objects collide (which is why I have one outside the if loop, and one inside) I never get any output to the console. What could be wrong?

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 vintar · Aug 01, 2016 at 08:17 PM

Collision will not be detected if isTrigger is checked.

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 nipunasudha · Aug 22, 2018 at 10:32 AM 0
Share

ooops! I honestly couldn't think of the reason until I saw this. Thank you.

avatar image
0

Answer by Unessential · Aug 01, 2016 at 08:24 PM

I intend to have the two bodies be able to overlap each other (hold the same space) on the screen and enter a specific state while it's held in this position. Also count how many times this state has been entered. How do I do that? If i uncheck "istrigger" the bodies literally collide and prevent each other from occupying the same 2D space. and if i code one body to enter the other it gets "ejected" out.

That is the situation when both bodies have "is trigger" checked (and the console prints). However, when one of them is checked and the other isn't. (or neither is checked) they are able to pass through each other again, but the console won't print.

tl;dr, how do I have detection as well as being able to have both objects hold the same space?

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 Unessential · Aug 01, 2016 at 08:57 PM 0
Share

After googling more about triggers I found that the constructor is different:

function On Trigger Enter2D(col : Collider 2D) {

$$anonymous$$y code is now:

 function OnTriggerEnter2D(col : Collider2D) {
     
    Debug.Log("collide1");
    
    if(col.gameObject.tag == "objecttag"){
        Debug.Log("collide2");
    }
 }

I've also numbered the two console prints to differentiate with them. Right now I can get 1 to fire but not two.

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

106 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

Related Questions

Ui Collision Detection 0 Answers

How to know when the player object falls between tiles in 2D game to take decesion of gameover 1 Answer

transform.position to Rigidbody.AddForce 2 Answers

boxCollider2D don't move with a tile object 1 Answer

Collision Problems 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