- Home /
Gameobject not detecting collison from other Box Collider 2D [SOLVED?]
Unity Version 2019.4.12f1
I am attempting to detect a collision from a Box Collider 2D that is turned on and off when the player uses a certain attack. I have tried everything I can possibly think of as to why this isn't working.
public void OnCollisionEnter2D(Collision2D col) {
if (col.collider.name == "Bullet(Clone)")
{
health -= 1;
}
if (health <= 0)
{
Destroy(turret);
Debug.Log("turret destroyed");
}
if (col.collider.name == "shieldSlam")
{
isStunned = true;
stunnedTimer = 3;
}
}
}
The bullet detecting works perfectly fine. It is only the third if statement that doesn't work. It is not the way it is written, as I have substituted other Colliders in it's place and it has functioned then.
I have tried: ensuring collision matrix is correct, ensuring one of them as a non-kinematic rigidbody, both have colliders, both have continuous collision detection, ensuring the attacks' collider is ENABLED at the proper timing for collisions, ensuring z-position is accurate on both, trying to find it by tag rather than name, renaming it, un-childing the attack collider from player.
I am unsure as to what else I need to possibly check. I feel like I have been staring at this small issue for nearly 2 hours.
Answer by fullofturtles · Dec 10, 2020 at 05:09 AM
I fixed it by using "OnTriggerEnter2D" and ignoring an if statement. For some reason finding it by name/tag was just not working, so I had to use it as a lone trigger. Still unsure as to why though.
Answer by theLameBrain · Aug 10, 2021 at 05:46 PM
@fullofturtles try using col.collider.gameObject.name?
Your answer
Follow this Question
Related Questions
2D collision won't work? 2 Answers
Trying to make object turn red OnTriggerEnter 0 Answers
get only one colliding body 3 Answers
Problem with method Collider2D.isTouchingLayers() 4 Answers
How do I use colliders and/or triggers to end the game? 1 Answer