OnTriggerExit2D somehow not being called

I have a mechanic in my game where I can pick up tiles and drop them on stacks of other tiles, sorta like playing cards. Because “stacks” are close enough together that a tile I’m dragging can overlap 2 stacks simultaneously, I use some simple toggles to determine if the tile I’m dragging is currently over one and/or two different stacks:

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.gameObject.tag == "Stack" && !onStackOne) {
			stackBaseOne = other.gameObject;
			onStackOne = true;
		} else if (other.gameObject.tag == "Stack") {
			stackBaseTwo = other.gameObject;
			onStackTwo = true;
		}
	}

	void OnTriggerExit2D(Collider2D other)
	{
		if (other.gameObject == stackBaseOne) {
			onStackOne = false;
		} else if (other.gameObject == stackBaseTwo) {
			onStackTwo = false;
		}
	}

Note that it’s not possible for a tile to be over 3 stacks at once, which is why I don’t check for !onStackTwo in the first else if statement.

In my OnMouseUp() method elsewhere in the script I check for various possibilities of onStackOne and onStackTwo being true, and then do accordingly to drop my tile on the right stack. The “stacks” are moving down the screen while this happens and if they get below a certain position then they shoot down way below the game screen and are destroyed after one second (they’re way off screen by the time they’re destroyed).

Everything works perfectly, except I occasionally run into an error where a tile I’m dragging has onStackTwo set to “true”, but StackBaseTwo is null because the object currently assigned to it has been destroyed. This is what I can’t figure out, because a stack is only ever destroyed after it shoots way down below the screen, so OnTriggerExit should have been fired long before that and onStackTwo should have been set back to false. But somehow, in some circumstance, the object assigned to stackBaseTwo is exiting the collider but NOT triggering OnTriggerExit, so when it’s destroyed I lose my reference to stackBaseTwo yet my onStackTwo variable is still “true”.

I should mention that I have multiple tiles running this script simultaneously, so each one has it’s own values for the onStack and stackBase variables. But I can’t see why that would make a difference here.

Any idea how this could be happening?

Alright, I’ve been trying to figure this out for 2 days and I just figured it out RIGHT after posting this. What are the odds!

The issue is that if you use Collider2D.size to resize a collider while it’s already inside a collider that is calling OnTriggerEnter, the resizing will RE-TRIGGER OnTriggerEnter as if the collider exited and re-entered. So in my case my stack is getting assigned as both stackBaseOne AND stackBaseTwo simultaneously.

Easy fix by simply checking that stackBaseOne != other.gameObject prior to assigning it to stackBaseTwo:

void OnTriggerEnter2D(Collider2D other)
{
	if (other.gameObject.tag == "Stack" && !onStackOne) {
		stackBaseOne = other.gameObject;
		onStackOne = true;
	} else if (other.gameObject.tag == "Stack" && stackBaseOne != other.gameObject) {
		stackBaseTwo = other.gameObject;
		onStackTwo = true;
	}
}

I’m assuming this is a bug in Unity, because I can’t imagine a case where you actually want to re-call trigger methods after resizing a collider.