attachedRigidbody null if GameObject hierarchy disabled

Can anyone tell me what’s the rationale behind setting attachedRigidbody to null anytime the collider is part of inactive hierarchy (possibly also when the component is disabled)? It sure doesn’t make developers life any easier. I can’t even use SetDensity() on an inactive GameObject’s rigidbody.

For 2D physics this method will return the Rigidbody2D on an active collider quickly however if the Collider2D is not active (either explicitly or because the hierarchy is inactive) then it will search the parent hierarchy looking for a Rigidbody2D. That search takes longer but is only done if inactive.

I guess you’re talking about 3D physics here; taking a look at the 3D code it doesn’t seem to have this fallback to show you what it would logically be attached to if it were active. Maybe there’s a reason for this but it’s likely that it’s just not been requested and therefore not a priority. @yant

As a workaround or to replace that property entirely, you can use GameObject.GetComponentInParent()

Hope that helps.

That’s not a work around for SetDensity() not working and I hope no one will tell me to calculate the collider mesh volume manually and derive the mass from it. Obviously I can set the GO to active temporarily but that surly comes at a cost.

I never said it was. I was obviously addressing a workaround to the “attachedBody” not being available in 3D physics when inactive but that’s useless to you because your post (apparently) is more focused on “I cannot call SetDensity() when the Rigidbody is inactive” instead.

I’m not a 3D physics dev but properties that are not directly exposed in the inspector are volatile and require the underlying body in the physics sub-system to be present which is only the case when active. From looking at the code, it sets the mass/inertia on a valid actor in physx but yeah, it needs to be around for that to happen.

Thanks for the reply.

Any chance some 3D physics dev weighs in on what would be the least bad way to go about setting density on inactive objects.

There isn’t a way AFAIK because the object isn’t around to do so. The only option I can think of would be for you to store this density value on your script and assign it when the GameObject (and script) becomes active.

There are some oddities which I don’t follow for inactive 3D rigidbodies so yeah, I did tag in a 3D physics dev above for you.