OnCollisionEnter2D and OnCollisionExit2D issue

Trying to have two colliders ignore each other if they are of similar color. If I have no oncollisionexit2d method the ignore collision effect is permanent, which I do not want. If i do have an oncollisionexit2d method, both the enter and exit methods are called before the collider is able to pass through, essentially “pushing” the moving collider out and not allowing it to pass through. Using boxcollider2d for door and circlecollider2d for player.

private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == “Door”)
{
print(“Player entered door.”);
objSpriteRenderer = other.gameObject.GetComponent();
objCollider = other.gameObject.GetComponent();
objColor = objSpriteRenderer.color;
if (Mathf.Abs(objColor.b - color.b) <= 0.05F)
{
Physics2D.IgnoreCollision(objCollider, playerCollider);
OnCollisionEnterTriggered = true;
}
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == “Door”)
{
if (!OnCollisionEnterTriggered)
{
print(“Player exited door.”);
Physics2D.IgnoreCollision(objCollider, playerCollider, false);
}
}
}

For everyone how wants to help…


Trying to have two colliders ignore each other if they are of similar color. If I have no oncollisionexit2d method the ignore collision effect is permanent, which I do not want. If i do have an oncollisionexit2d method, both the enter and exit methods are called before the collider is able to pass through, essentially “pushing” the moving collider out and not allowing it to pass through. Using boxcollider2d for door and circlecollider2d for player.


private void OnCollisionEnter2D(Collision2D other) 
	{ 
	if (other.gameObject.tag == "Door") 
	{
		print("Player entered door."); 
		objSpriteRenderer = other.gameObject.GetComponent();
		objCollider = other.gameObject.GetComponent();
		objColor = objSpriteRenderer.color;

		if (Mathf.Abs(objColor.b - color.b) <= 0.05F) 	
		{ 
			Physics2D.IgnoreCollision(objCollider, playerCollider); 
			OnCollisionEnterTriggered = true; 
		} 
	} 
} 


private void OnCollisionExit2D(Collision2D other) 
	{ 

	if (other.gameObject.tag == "Door") 
	{ 
		if (!OnCollisionEnterTriggered) 
		{ 
			print("Player exited door."); 
			Physics2D.IgnoreCollision(objCollider, playerCollider, false); 
		} 
	}
}


I think something like this should work:


  private void OnCollisionEnter2D(Collision2D other)
    {
        if((other.gameObject.tag== "Door") && (gameObject.GetComponent<SpriteRenderer>().color == other.gameObject.GetComponent<SpriteRenderer>().color))
        {
            print("Wow");
            Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), gameObject.GetComponent<Collider2D>());
        }
    }

I hope I did not miss anything, and hope I could help out…

Have a nice day
Greetings,

WbrJr