- Home /
Why is my Collider Broken?
Hey Comm-Unity, I usually consider myself to be a very well-rounded and developed Unity user, however, I am having issues with a collider. What I have is a simple 2D Game, and I'm trying to make a UI Image appear when a sprite collides with another sprite. Both sprites have 2D Box Colliders, One has Is Trigger set to true, and the other has a 2D Rigidbody attached. I am using the following code:
void OnTriggerEnter2D(Collider2D c)
{
if(c.tag == "Other")
{
Panel.SetActive(true);
}
}
Can anyone think of the problem? Thank you!
Tiger27
The code itself seems pretty bullet proof. I would add a debug before the if to see if OnTriggerEnter2D gets called at all. If it does get called it might just be that the object co$$anonymous$$g in doesn't have the right tag. If it's not getting called at all then it probably has something to do with the collider or the rigidbody. Can we get a screenshot of the collider setups in the inspector?
Hey @TS125 and @hexagonius, Thanks for the quick reply. As for the if statement, I already did, and it isn't logging anything. Another thing I did was add the following code, to see if it's calling a collision ins$$anonymous$$d:
void OnCollisionEnter2D(Collision2D c)
{
Debug.Log("Collision!");
}
As for the rigidbody, I am moving it with Vector2.$$anonymous$$oveTowards (Or maybe Vector3.$$anonymous$$oveTowards, don't have my code in front of me). Does that affect anything? Thank you for your help!
Tiger27
you changed it to OnCollisionEnter. Is it a collider now?
$$anonymous$$oveTowards teleports the object, which is the reason for the missing call. if the rigidbody is kinematic use $$anonymous$$ovePosition on the Rigidbody.