Help - 2D Trigger not detecting collision

Hi, I’m making a simple 2D flappy bird game from a tutorial as my first Unity project. If it matters, here is a link to the tutorial. I have a trigger collider that sits between the top and bottom pipes to detect when the bird goes between the pipes. It’s supposed to use the OnTriggerEnter2D function to do this. However, the OnTriggerEnter2D function never gets triggered. Here is a picture of the relevant code. I can send more information if required. Neither of the debug log statements are triggered when the bird goes through the collider. Layer 3 is the layer the bird is on and logic refers to my logic manager script.

Check out the specific list of requirements in the docs for OnTriggerEnter2D (and more notes in the Physics 2D area) and then make sure that this tutorial a) meets those requirements (it probably does), and more importantly b) that you have done ALL The parts of the tutorial necessary to meet those requirements by the time you expect it to work.

If you miss even one requirement, it will not work.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

That’s some very nice learning advice. I have already looked at the Unity Docs for OnTriggerEnter2D and have not been able to figure out what the issue is. I am indeed using debug statements, and, I as I said earlier, they show that the OnTriggerEnter2D is not triggering. Do you know why this might be?

Nope, but you can find out by debugging above. Is the script running? Find out with Debug.Log() in the Start(). Is ANYTHING being called, even if the layer is wrong? Find out with more Debug.Log() in the trigger callback function.

If you doubt that it works at all, Unity makes this SUPER easy to prove.

  • drop a sprite in the scene, put a BoxCollider2D and Rigidbody2D on it
  • drop another sprite in the scene, slide it below the above
  • make sure this has a BoxCollider2D as well and make sure they don’t already overlap
  • make a tiny script with just the trigger responder function and Debug.Log(), put it on the first Sprite.
  • make sure the second BoxCollider2D is marked trigger
  • run

This stuff does work, so doublecheck your work against the docs and when you get a small thing working, just bisect back to what you have to find which part you’re missing.

The script isn’t running, which I know because of the Debug Log tags that I put in to test that exact point before making this post. The docs don’t seem to say anything helpful.

Excellent! Now check the list of requirements for a script to run: naming, what it must be attached to, what it derives from, etc.

Where is this list of requirements? I don’t know of such a list.

The docs show what is required to get OnTriggerEnter2D to run: Unity - Scripting API: MonoBehaviour.OnTriggerEnter2D(Collider2D)

Quick glance at the video shows me he doesn’t do anything wrong, so you have missed some step of the process.

2 Likes

Huh. The only requirement I see listed there is that one of the colliders has a rigidbody2d attached, which it does.

Have you checked if there’s a rigidbody2D in it? If not, add it. It worked for me when i was struggling finding a solution.

There is, but thanks for the thought :slight_smile:

Can you send a screenshot?
Maybe i can look deeper in the photo to see the awnser.

Did you… remember to attach your custom monobehaviour to the trigger receiving game object?

Have you accidentally added a 3d collider rather than a 2d collider?