OnTriggerEnter2D not work on certain layers

Title describes the problem.

  • all filters disabled
  • no call Physics2D.IgnoreCollision anywhere
  • Depth same, z-position = 0
  • Empty project not reproduce problem
  • switch trigger to layer 7, 14+ = no log
  • 1-6,8-13 layers = hit log
  • tried override layers → include Everything = still layer effect log

Setup:

Code
	public class Ladder : MonoBehaviour
	{
		private void OnTriggerEnter2D(Collider2D other)
		{
			Debug.Log($"OnTriggerEnter2D {other.name}");
		}

		private void OnTriggerExit2D(Collider2D other)
		{
			Debug.Log($"OnTriggerExit2D {other.name}");
		}
	}
  1. What I’m doing wrong?
  2. Can I somehow reset internal data for physics2D? (reimport all?)

There’s your clue. Something in your setup is broken. You could try to reimport all, but that’s unlikely to fix it.

Start with an ULTRA simple test script with a trigger and prove it works in this project.
Sounds like you wrote a bug… and that means… time to start debugging!

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.

1 Like

Thank you for answer, but please check out code i’ve posted, there is minimum code and log, no conditions

That’s a snippet and wouldn’t help anyone debug your project unfortunately. If the two Rigidbody2D coming into contact have body-types that are allowed to contact, are on a GameObject with a Layer that is set to contact, one has at least a single collider that is set as Trigger and one has the “Ladder” script on it then those callbacks will happen.

This is a fundamental piece in 2D physics, it’s highly unlikely it’s broken and certainly hasn’t changed in many years. The layer collision matrix is read directly from the “ProjectSettings / Physics2DSettings.asset” which also contains the other page of settings. That gets reset if you delete it or hit reset in the settings window you’re showing. As you can see there’s also a “Disable All” and “Enable All”.

Not sure what you mean by this.

It’s 2D physics, Z-position is irrelevant.

Clearly shows it’s your project. Nothing else is persisted.

That clearly shows it’s nothing to do with Layer Collision Matrix then because this will completely override it and is an entirely different part of the code.

1 Like

That is not how debugging works, as Melv points out.

If it was, we could just glance at stuff and fix it and be done.

Code is irrelevant. Repeat that after me. Code DOES NOT MATTER.

For instance, how important is the code if it isn’t even being called?

What the code is doing and what the data the code is transforming matters.

Find out what your code is doing by debugging. I’ll post it again:

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.

problem was in ProjectSettings/Simulation Layers

1 Like

That defaults to “Everything” so you must’ve changed it at some point.