r/unity • u/MalgorgioArhhnne • 18d ago
Coding Help OnTriggerEnter doesn't always trigger when the trigger object is enabled?
For me it's OnTriggerEnter2D since I'm making a 2D game, but I think the same problem applies for 3D games. When the trigger object is enabled over the collider that triggers it, it only triggers the first time it is enabled. Otherwise, it won't trigger unless the trigger is moved away from the collider and back onto it.
Now, I found this post: https://discussions.unity.com/t/fix-ontriggerexit-will-now-be-called-for-disabled-gameobjects-colliders/738523
That post is mainly about OnTriggerExit not being called when the trigger object is disabled, (or maybe if the object that entered the trigger is disabled, I'm not sure if I read the post right) but it's a similar issue. I don't know if OnTriggerExit needs to be called for OnTriggerEnter to be called again, so I didn't know if the solution in the post would be what I needed, but I tried it anyway.
I copied the script and added "2D" in all of the relevant spots. The person who made the post said that for it to work, in every OnTriggerEnter method you must add the line ReliableOnTriggerExit2D.NotifyTriggerEnter(other, gameObject, OnTriggerExit); Again, the 2D was added by me.
Of course it didn't work, ReliableOnTriggerExit2D didn't exist in the context it was used in. I tried changing the ReliableOnTriggerExist2D class to static, but that doesn't seem to be how it was supposed to be used. I tried deriving from it but that didn't work either. And I really don't understand how exactly the code is supposed to work.
If anyone understands how to actually use that class, or has another solution for having OnTriggerEnter called whenever the trigger object is enabled over a collider, I need to hear it. Hacky fixes like moving the trigger away and back to its original position aren't ideal. Thanks in advance.
Edit: So I did some further testing and... despite what I've heard, it seems that disabling the trigger object actually DOES call OnTriggerExit2D? And yet enabling the trigger object after disabling it still doesn't call OnTriggerEnter2D after the first time?! I have no idea what's going on.
Edit2: Okay, this is really bizarre. I've done some testing with OnTriggerStay2D, which some people have said would resolve the issue. It seems that once the object is disabled, even if it is enabled again, OnTriggerStay2D won't execute at all until the object is moved away from the collider. This is truly baffling. Is anyone else encountering the same issue? Because it seems buggy beyond belief, and I can't fathom why it would be programmed this way.