- Home /
Parenting an object causes Collider2D to stop working
Hey there, I have a question about parenting and colliders.
I have player, box and wall game objects. Both player and box have their own box collider 2D and rigidbody, both dynamic (at first) and both work just fine and are confined within the walls no problem.
However, I implemented a grabbing function in which the player grabs the box while a key is pressed. I'm using this code to do that:
if (m_IsHoldingGrab)
{
grabbed.transform.position = transform.position;
grabbed.transform.parent = transform;
grabbed.GetComponent<Rigidbody2D>().isKinematic = true;
}
else
{
grabbed.transform.parent = null;
grabbed.GetComponent<Rigidbody2D>().isKinematic = false;
}
Grabbing works just fine, but suddenly my box can't collide with anything while it's parented to the player. Is there a way to make it still collide even thought it's parented?
P.S.: Here's the Rigidbody2D definitions for my box and player:
----Box: 
----Player: 
Thanks!
Well the parenting probably isn't your issue here, but this might be:
grabbed.GetComponent<Rigidbody2D>().is$$anonymous$$inematic = true;
From the documentation: If is$$anonymous$$inematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.
@TreyH is correct as this may be the issue. Setting an object to $$anonymous$$inematic makes it so that it doesn't react to physics.
Another potential issue is that I see you're moving the object using its' transform. You'll want to use Rigidbody2D for this; directly moving a transform will place the object wherever you tell it without taking collision into consideration.
For further reading: https://answers.unity.com/questions/215377/transformtranslate-vs-rigidbodymoveposition.html
Answer by rogvc · Jan 06, 2020 at 05:50 AM
Thanks for the replies! I fixed it by not changing the rigidbody's body type at all and changing the gravity scale instead. Works like a charm now.
Your answer