Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
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
12

Answer by abdextrous · Mar 15, 2017 at 09:23 AM

In my case it was the most stupid one I guess (Sorry a newbie in CS, and also was following a poor quality video tutorial !). I had a typo mistake, made it onTriggerEnter2D instead of OnTriggerEnter2D :(

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 bibbis · Mar 19, 2017 at 08:05 PM 0
Share

Didn't you get corrected by the program you're coding in?

avatar image RobAnthem bibbis · Mar 19, 2017 at 09:56 PM 1
Share

Not for functions like these. Essentially if it isn't an override function, then what Unity does is it scoures the class file for all functions, and if one of those compares to a default function, it adds it to the event handler of the specific events. In other words, it doesn't care what you call your methods, but if it is a default one, it will run it, otherwise only your code will run it when you call it. There is no "error" to be had here.

avatar image two2uns · May 14, 2021 at 01:29 PM 0
Share

... also check if your parameter is correct. For example, it needs to be a Collider2D for OnTrigger...2D, not a Collider.

avatar image Kiaratto · Nov 20, 2021 at 03:06 AM 0
Share

Good this is my case kk !! The code pattern is different and confusing in unity. Becaus has a many different patterns in world ok. But commonly the programers using camelCase. I prefer UpperCamelCase its more legibly and intuitive. But Unity merge the two styles it's very confusing

avatar image schwukjr · Nov 28, 2021 at 08:03 AM 0
Share

I've just spent the better part of an hour trying to figure this issue out, and your comment was a god-send. Thank you!

avatar image
5

Answer by RogerDebian · Apr 28, 2017 at 05:51 AM

I forget the damn Rigidbody2d. In my case, my player has a Character Controller, so, there is a problema between the Rigidbody2d and the Character controler, so, I made a new gameobject with a rigidbody2d and a box collider and BOM! solution!

thanks for your research! I forgot the behavior of the components in this case! :D

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
2

Answer by SoliderSpy · May 01, 2018 at 09:11 PM

You don't need to make gravity scale 0. Put a rigidbody2d on both of the objects and then you will be able to make them both kinematic with the collision or trigger working. :)

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 Nairda_Draziw · Jul 21, 2018 at 07:06 PM 0
Share

Thank you. Worked for me. However, make sure "simulated" box is ticked.

avatar image
1

Answer by hersheys72 · May 02, 2016 at 01:09 AM

Thanks for your answer. For mine the box collider size had defaulted to 1 by 1, when the object itself was 400 x 400, so it was never colliding. Also had to change the offset to match the objects location but works fine now :)

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.

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

Collision with renderer.enabled? 0 Answers

Fitting 2D collider to a sprite 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