【Unity】Can’t destroy Transform component of ‘〇〇〇〇’

以下のエラーメッセージが表示されてしまった。

Can’t destroy Transform component of ‘〇〇〇〇’. If you want to destroy the game object, please call ‘Destroy’ on the game object instead. Destroying the transform component is not allowed.

transformオブジェクトはDestroyできないから、GameObjectを代わりにDestroyしろとのこと。

なので

foreach (Transform n in gameObject.transform){
GameObject.Destroy (n);
}

から

foreach (Transform n in gameObject.transform){
GameObject.Destroy (n.gameObject);
}

に変更

TransformはGameObjectを継承しているのかな?

とりあえずTransformはgameobjectをアタッチしているようなのでこの修正でエラーメッセージが消えました。