• 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
17
Question by arkariarn · Dec 08, 2013 at 10:13 PM · 2dcolliderspritetriggerboxcollider

why isn't my OnTriggerEnter2D() function working?

guys im struggling over an hour on the 2d collision event. Currently I have 2 sprite's each have a 2d boxcollider and are triggers. I have set the tag of one of the sprite's to "player" but it still doesn't work. I have already checked everything ten times and searched the internet for a solution.....without succes.

please help a fellow unity for the sake of the community xd

btw both sprite's are animated, but I don't think that's the problem...

 using UnityEngine;
 using System.Collections;
 
 public class collision : MonoBehaviour {
 
 
 
     void OnTriggerEnter2D (Collider2D other)
     {
         if (other.gameObject.tag == "Player") 
         {
             Debug.Log ("why wont you work ;_;");
         }
     }
 }


[1]: /storage/temp/19090-capture2.png

capture2.png (55.1 kB)
capture.png (45.6 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

14 Replies

· Add your reply
  • Sort: 
avatar image
92
Best Answer

Answer by arkariarn · Dec 08, 2013 at 11:31 PM

After hours of testing i finally got it to work here is how:

first you need one of the two objects to contain a 2d rigidbody. While i was already doing this it still didn't work. In my game i'm a bird so I didn't want gravity to effect my player so I had set the kinematic to true. This caused the object to ignore collision events.... so I just set the gravity scale of the object to zero and made the object dynamic (not-kinematic).

this fixed my problems... hope it helps somebody.....ever....

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 magique · Jan 14, 2015 at 06:39 PM 0
Share

Another potential gotcha that I have encountered with collision triggers not working is if the Physics2D settings don't have interactions turned on for the the layers in question. So, for example, if you have objects in user defined layers such as Player and $$anonymous$$issile, but you don't have the check box marked for Player/$$anonymous$$issile collisions, then no triggers will be called. You can check your settings in Edit/Project Settings/Physics2D.

avatar image E3S21I · Jan 18, 2015 at 01:51 PM 0
Share

Still doesnt work for me, its set for 2D an using js, is trigger has be ticked for the chosen ones, they all have a Rigidbody2D and a circle collider, scripts all '2D's at the end of 'collider' and 'OnTriggeEnter-Exit'

What can I do?

avatar image E3S21I · Jan 18, 2015 at 02:42 PM 0
Share

Never$$anonymous$$d, is trigger needed to be unticked :)

avatar image schouffy · May 11, 2015 at 09:04 AM 0
Share

This is stupid ; I had the same problem (two collider2D, one was as trigger). I fixed it by adding a rigidbody2d component to one of my colliders. I don't even need any rigidbody behavior. Anyone knows why this is necessary ? Can't we just have colliders collide/trigger by themselves without rigidbody involved ?

avatar image haidao0923 · Jul 24, 2018 at 10:35 PM 0
Share

kinematic means it is not affected by forces period not just gravity

avatar image
0

Answer by grange9 · Jun 09 at 12:54 PM

Mine still does not work even though I have followed all your instructions/ Ideas and the OnTriggerEnter2D still does not work. I do not have any typos, there is already RigidBody2D AND it is not in kinematic mode. Are there any more suggestions to help with my code?,I put In everything Including RigidBody2D( not the kinematic one) And I took all the help from above. Even so my code/detection still does not work. Does anybody know how to help me further?

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 qinyupeng · Dec 15, 2021 at 01:43 AM

Believe it or not, In Unity2021

  void OnTriggerEnter2D(Collider2D other)
  private void OnTriggerEnter2D(Collider other) 

is a different function. Unity will call the first one but not the second one. I found this problem after 2 hours checking.

if you just want to check the collider, you don't have to add Rigidbody. just select isTriger on BoxCollilder.

Also, remember you are using the 2D function or the 3D function.

hope my experiences will help others.

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 Max_power1965 · May 24, 2020 at 03:49 PM

I also had really a lot of problems when configuring the collision between game objects. After years I wrote every tips in this article https://gamedevelopertips.com/unity-collision-detection-2d/, I'm sure it'll be helpful if you want to set up properly you collider/triggers properly.

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
1

Answer by ArikShemesh · May 01, 2019 at 08:33 PM

For me it was a stupid mistake but tricky to find. One of my objects had the "Box Colider 2D" and "Rigibody 2D" set to Kinematic. The other object was also had the box colider and Is trigger was set, but the problem was that it was "Box Colider" and not "Box Colider 2D".

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
  • 1
  • 2
  • 3
  • ›

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

Why doesn't OnTriggerEnter2D get called? 1 Answer

Wanting to have a sprite animation to only start playing once the player collides with it? 1 Answer

Unity2D: Renderer off/ on by triggers 0 Answers

How can a character not move out of box collider's bounds? 1 Answer

Prevent a collider from affecting the rotation of a sprite,Prevent a collider from affecting players's rotation 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