- Home /
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() + "\n";
}
}
}
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?
Does not work means what?
Is the required RequireComponent found calling comp.GetType().GetCustomAttributes(typeof(RequireComponent)
?
Sorry I should have been more specific. For custom scripts the component type that is required gets added to the list, but for some reason with internal components, their required type is not added to the list
Answer by Bunny83 · Oct 31, 2018 at 02:22 AM
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)
// [ ... ]
That fixed my issue, thank you! I can't believe I missed that
Your answer
Follow this Question
Related Questions
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer
Confused about custom GameObjects,Custom GameObject confusion 0 Answers
Get callback when assets loading or deserialization finished? 1 Answer
Reorderable list of UnityEvents 2 Answers
Gizmos icons 0 Answers