- Home /
Could GameObject have multiple bodies?
Hello all,
I was going through the Unity3D docs and some google search but can not find the answer and understand the mechanics of adding multiple components to GameObject. The whole concept is simple - just use AddComponent<$ComponentType$>() as much as you like and fill your GameObject with features and traits, however there seem (in my view) to be some 'special' cases like this one f.e.:
Could I add more then one Rigidbody2D to single GameObject?
If I can then what of multiple Rigidbodies will return Collider2D.attachedRigidbody when I'll add some? If not then where I could find the info about special cases like that one? Are there any?
Also what will happen if I add Collider2D to GameObject with no body ( Collider2D.attachedRigidbody will be null or same as parent body) and then attach Rigidbody2D? Would the Collider2D.attachedRigidbody change?
Answer by TreyH · Mar 11, 2019 at 03:03 PM
Objects can have 1 Rigidbody at-most, but an object can have multiple colliders.
If your goal is to have multiple Rigidbody components governing a "single" object (one limb heavier than another), then you could just attach one to each limb.
You can also check your question pretty easily:
public class ColliderRigidbodyCheck : MonoBehaviour
{
void Start()
{
var col = this.GetComponent<Collider>();
Debug.LogFormat("[1] Attached rigidbody is null? {0}", col.attachedRigidbody == null);
var rb = this.gameObject.AddComponent<Rigidbody>();
Debug.LogFormat("[2] Attached rigidbody is null? {0}", col.attachedRigidbody == null);
DestroyImmediate (this.GetComponent<Rigidbody>());
Debug.LogFormat("[3] Attached rigidbody is null? {0}", col.attachedRigidbody == null);
}
}
Which shows that the attachedRigidbody does reflect changes with each call:

So is this the only case for unique component in GameObject? Where could I get this information? Or should I "check" any of my basic questions by trial and error in Unity?
PS: Sorry for the delay in response.
Your answer
Follow this Question
Related Questions
have basic controllers, need flying 1 Answer
Is it right to turn false both navmeshagent updateRotation and updatePosition properties ? 0 Answers
How to modify prefab permanently via script. 0 Answers
Is there a way to find the rest positions/orientations of bone transforms of an imported object? 1 Answer
How can i get raycast hit from a specific gameobject ? 1 Answer