Search code examples
unity-game-enginereflectionattributes

Only one Rigidbody can be added to a GameObject, yet Rigidbodies lack [DisallowMultipleComponentAttribute]. Why?


Obviously GameObjects should not have more than one Rigidbody. Yet, after digging through the source code, I don't see any attribute that would cause the behaviour.

namespace UnityEngine
{
...
    [RequireComponent(typeof(Transform))]
    [NativeHeader("Modules/Physics/Rigidbody.h")]
    public partial class Rigidbody : UnityEngine.Component
    {
    ...

How does UnityEngine know to not allow more than one Rigidbody on a GameObject? Is it in Rigidbody.h, or perhaps in some other editor script? I would like to determine if a component can be added to a GameObject using reflection, which is why I'm looking for a [DisallowMultipleComponent] attribute and am discombobulated by its absence.


Solution

  • As it says in the Readme of your referenced repository, this is not the full source code of the Unity Engine. It's only the C# part (not the C++ part). Also the class definition uses the partial keyword. Which means there might be additional parts of this class definition elsewhere.

    If you want to look at the full souce code you'd have to email Unity directly.

    I'm sure you know this, but one should try to avoid using Reflection and use [DisallowMultipleComponent] instead if possible.