Question by
roman_sedition · Aug 19, 2021 at 04:44 AM ·
destroymonobehaviourcomponentsattributerequired
Destroy components sequentially if it has Required Component Attribute
Let's say I have a Rigidbody on a gameobject I want to destroy, but another component on that gameobject requires the Rigidbody.
Is there a way, without having to know what component's Required Components dependency are, to sequentially destroy any Required Components before destroying my targeted component to destroy?
e.g I want a simple method like
public void DestroyComponentTree(monobehaviour behaviour)
{
var requiredComponents = behaviour.GetRequiredComponents();
foreach(var item in requiredComponents)
{
Destroy(item);
}
Destroy(behaviour);
}
Although I guess it probably runs into issues if you have multiple of the same type of required component on the gameobject but you only want to destroy the one which is associated with intended component you want to destroy.
It's just annoying having to code a manual sequential destroy of components, would prefer a 1 fits many solution instead.
Comment