# OnTriggerEnter2D and OnTriggerExit2D are firing each frame of a collision and OnTriggerStay2D never fires

So I'm basically making pong and lethal league combined, the paddles start with no collision until a certain key is pressed then the collider is enabled. OnTriggerEnter2D stops the ball, OnCollisionStay2D adds to an acceleration variable while the ball is still colliding, and OnTriggerExit2D flips the direction of the ball and applies the acceleration.

The problem I'm having right now is that Enter and Exit are being called every frame of the collision instead of the first frame and the frame after collision respectively, and Stay is never called at all, but that part is understandable.

Even if I comment out the OnTriggerExit2D function, which again applies the new speed and flips the direction, and essentially make the ball stop on collision, OnTriggerEnter2D will still fire each frame despite nothing moving.

I tried taking this to my professor but he wasn't of much help, I don't think I'll be able to get any help on this unless it's from here or I take my laptop to his office to show him what's going on. Please help

void OnTriggerStay2D(Collider2D c)
{
if (c.gameObject.transform.tag.StartsWith("Paddle") && acceleration <= 1)
{
acceleration += 0.1f;
}
}

void OnTriggerExit2D(Collider2D c)
{
if (c.gameObject.CompareTag("PaddleLeft"))
{
dir.x = 1;
speed = tempSpeed + acceleration;
}

if (c.gameObject.CompareTag("PaddleRight")
{
dir.x = -1;
speed = tempSpeed + acceleration;
}
}

void OnTriggerEnter2D(Collider2D c)
{
if (c.gameObject.transform.tag.StartsWith("Paddle"))
{
tempSpeed = speed;
speed = 0;
acceleration = 0;
}
}

• Hey, the methods themselves might not be very helpful if you want to solve the trigger part, what are the settings you have on the game objects in question? And did you debug with breakpoints? Btw you wrote OnCollisionStay2D at the start of the question but used OnTriggerStay2D elsewhere. If you want collision detection you should use collision methods instead of triggers Mar 12 at 23:24
• Can you show how you're moving the objects here? My guess is you're using the Transform component instead of the Rigidbody2D. Moving a physics collider with its transform forces it to be removed from the physics scene (causing OnTriggerExit2D to fire) then re-added in the new place (causing OnTriggerEnter2D to fire), so it never stays in contact, it's constantly teleporting in & out. This is one of the reasons why you should avoid moving a collider via its transform. Mar 14 at 1:28

## 2 Answers

Always start by logging. If you try to infer which functions are being called based on the behavior you see in the game, you may misunderstand what is happening.

For example:

void OnTriggerStay2D(Collider2D c) {
Debug.Log(\$"{this} OnTriggerStay2D N:{c.name} T:{c.tag}", this);
if (c.gameObject.tag.StartsWith("Paddle") && acceleration <= 1) {
acceleration += 0.1f;
}
}


Then in the console, you'll see a message like

ball OnTriggerStay2D N:paddle T:PaddleLeft

Adding a similar log message for each trigger function will help you understand exactly what is happening.

I don't think it will affect functionality, but for consistency, you should access the tag from the same place. Right now you do c.gameObject.CompareTag() in some places and c.gameObject.transform.tag in other places. You can simplify the latter to c.gameObject.tag, or simplify even further and use c.tag and c.CompareTag().

• A lot of the code was done in class. Specifically the parts with the tags so I just took all of it as a starting point and didn’t want to refactor too much but I do appreciate it. I actually found out my issue earlier this morning. My issue wasn’t the OnTrigger functions, aside from a few changes after the fix they behave as intended. My problem was actually cause from how I was enabling and disabling the colliders on the paddles. Mar 14 at 4:35
• I was setting the colliders to false in the update function and then using Input.GetKey to detect if the key is pressed. So Unity thought it was a new collision each frame because it was. I fixed the issue by simply moving the lines of code that were disabling the colliders into else statements after the if statements containing the GetKey Mar 14 at 4:36
• If you solved your problem, @TyPe23, please post your solution as an Answer below. And also edit your question so that the code needed to reproduce the error is visible in the question itself. Mar 14 at 11:43

My issue wasn’t the OnTrigger functions, aside from a few changes after the fix they behave as intended. My problem was actually cause from how I was enabling and disabling the colliders on the paddles. I was setting the colliders to false in the update function and then using Input.GetKey to detect if the key is pressed. So Unity thought it was a new collision each frame because it was. I fixed the issue by simply moving the lines of code that were disabling the colliders into else statements after the if statements containing the GetKey

• Make sure the enabling/disabling code is shown in your question. For questions about debugging a problem in a game project, it should always be possible to reproduce the problem using the code in the question alone. Mar 15 at 11:59