A small game Component that holds a reference to another GameObject called Attachment and ensures it is destroyed when this component is destroyed; if the attachment becomes invalid during updates the component destroys its own owning GameObject.
internal class ConstraintCleanup : Component
{
[Property]
public GameObject Attachment { get; set; }
protected override void OnDestroy()
{
if ( Attachment.IsValid() )
{
Attachment.Destroy();
}
base.OnDestroy();
}
protected override void OnUpdate()
{
if ( !Attachment.IsValid() )
{
DestroyGameObject();
return;
}
}
}