myObjetc..GetType().GetCustomAttributes(typeof(RequireComponent) not working correctly

if (comp.GetType().GetCustomAttributes(typeof(RequireComponent), false).Length > 0)
{
bool contains = false;
foreach (RequireComponent required in comp.GetType().GetCustomAttributes(typeof(RequireComponent), false))
{
if (required.m_Type0 != null && !requiredComponents.Contains(required.m_Type0)) requiredComponents.Add(required.m_Type0);
if (required.m_Type1 != null && !requiredComponents.Contains(required.m_Type1)) requiredComponents.Add(required.m_Type1);
if (required.m_Type2 != null && !requiredComponents.Contains(required.m_Type2)) requiredComponents.Add(required.m_Type2);
if(((required.m_Type0 != null && required.m_Type0 != typeof(Transform))|| (required.m_Type1 != null && required.m_Type1 != typeof(Transform))|| required.m_Type2 != null && required.m_Type2 != typeof(Transform)) && !contains)
{
contains = true;
requirees += comp.GetType() + "
";
}
}
}

this function is adding components that are required to a list and it works perfectly for custom scripts with the [RequireComponent(Type)] however it does not work for any of the built-in components that require other components. For example the Fixed Joint component requires a Rigidbody. I thought that perhaps it wasn’t working because maybe their scripts do not contain a [RequireComponent(Type)] but instead were requiring it somehow else, but, I recently got ahold of Unity’s Editor Source code in which at the top of the script it reads [RequireComponent(typeof(Rigidbody))]. Does anyone know how I can fix it not working for unity components?

Uhm The FixedJoint class does not have a RequireComponent attribute, however it’s base class Joint has this attribute. Though for some reason you specifically passed “false” into the inherit parameter of “GetCustomAttributes” so it only checks the attributes on the type itself and not the attributes on any base classes. It seems that’s what you actually want, so all you have to do is pass “true” instead of “false”.

Note that you really want to store the result of GetCustomAttributes in a variable and not calling it several times

var attr = comp.GetType().GetCustomAttributes(typeof(RequireComponent), false);

if (attr.Length > 0)
{
    bool contains = false;
    foreach (RequireComponent required in attr)
    // [ ... ]