Question regarding the internals of Collider(2D).attachedRigidbody

Is Collider.attachedRigidbody and Collider2D.attachedRigidbody returning an internal variable or is it doing GetComponent() underneath?

The reason I asked is whether the following code should be written like this:

[...]
Foo(coll.collision.attachedRigidbody);
Bar(coll.collision.attachedRigidbody);
[...]

or like this:

[...]
var body = coll.collision.GetComponent<Rigidbody>();
Foo(body);
Bar(body);
[...]

Inspecting the UnityEngine Assembly yields the following answer:

public extern Rigidbody attachedRigidbody
{
    [WrapperlessIcall]
    [MethodImpl(MethodImplOptions.InternalCall)]
    get;
}

Internal variable.

1 Like