C# OnCollisionEnter2D failing to respond
Hello fellow unity patrons, I know this question has been asked before, but here we go. I want my 2D tank to take damage upon collision with barbed wire. on the tank i'm using a simple piece of code:
Void OnCollisionEnter2D(Collision2D col) { if(col.gameObject.tag == "BarbedWire") { Health--; } }
Like i said, should be very simple. The Tank has a Rigidbody and a polygon collider2D. The barbed wire has a polygon collider2D (IsTrigger false). I have tried attaching a script to the BarbedWire to nudge it ever so slightly to keep the collider working. I have looked through every place i could find for the answer, but none have fixed it. it simply does not register that it collided with the BarbedWire.
Thanks in advance, Rocketman.
Yes, the rigid body is 2D. I should have stated that, but you can't (at least in 2017.1) have a rigid body 3D and a 2D collider
Thanks for the quick response!
Answer by BreakTheWInd · Jan 14, 2018 at 02:15 PM
If the collider is on a child and the OnCollision is on the parent, try adding the following code to the child:
void OnCollisionEnter2D(Collision2D collision)
{
transform.parent.gameObject.SendMessage("OnCollisionEnter2D", collision);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612133136im_/https://answers.unity.com/themes/thub/images/avi.jpg)