Components/ConstraintCleanup.cs

A small component that keeps a linked GameObject in sync with its owner. It destroys the Attachment when this component is destroyed, and it destroys its own GameObject if the Attachment becomes invalid during updates.

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;
		}
	}
}