Search Issue Tracker
Fixed in 2022.2.13f1, 2023.1.0b10, 2023.2.0a8
OnTriggerExit2D not called when colliders are disabled due to getting unloaded
1. What happened
This project has the 2D setting "Callbacks On Disable" turned on. This setting is documented as:
"If the Collider2D being disabled has at least a single contact with another Collider2D then with this property set to true, a callback would be produced.
Only "OnCollisionExit2D" or "OnTriggerExit2D" will be called by disabling a Collider2D."
As expected, deactivating a GameObject when it's overlapping a different GameObject, and both have Collider2D's results in OnTriggerExit2D. But if I instead deactivate the GameObject by unloading the scene it is in, no OnTriggerExit2D is invoked.
Since both OnDisable and OnDestroy is called on objects when their scene is unloaded, my assumption here was that OnTriggerExit2D also should be called, but that is not the case.
This bug was originally found in Unity 2021.3.16f1, so it's been around for a while
2. How can we reproduce it using the example you attached
- Load Scene A, and enter play mode
- Use the "Load Level" button to load Scene B
- Move the black box so it intersects with the red box with A/D or the arrows, and note that "On Trigger Enter 2D" is printed to console.
- Use the "Deactivate Object" and "Activate Object" buttons to turn the red box on and off. Note that "On Trigger Enter 2D" and ""On Trigger Exit 2D" is printed to console
- use the "Unload Level" and "Load Level" buttons again to unload and load Scene B. Note that "On Trigger Exit 2D" is _not_ printed to console, while "On Trigger Enter 2D" is!
- expected behaviour is that unloading the scene should cause an OnTriggerExit2D to be raised.
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
- SHADERPASS has incorrect value when using Shader Graph for Built-In target
- [VFX] Gizmo still incorrectly rendered for a hidden property
- Nested object label indentation is incorrect when using the UI Toolkit Inspector
- Crash on tlsf_free when UI Elements are moved to the side by scrolling
- Crash on AudioUtil_CUSTOM_HasAudioCallback when entering PlayMode while the GameObject's Properties Inspector window is in Debug mode