利用循环销毁物体和子物体:
for循环:
Transform[] xxxx = transform.GetComponentsInChildren<Transform>();
//定义一个内容为该游戏物体所有组件的数组
for(int i=0;i<children.length;i++)
{
if(children[i] !=transoferm)
{
GameObject.Destroy(children[i].gameobject);
}
}
//销毁游戏物体的子物体
利用while循环:
int i = 0;
while (i < children.Length)
{
if(children[i] != transform)
{
GameObject.Destroy(children[i].gameObject);
}
i++;
}
do while循环:
do
{
//循环体
}
while(//循环条件)
//先执行循环体在判断满足条件,若满足则再次执行循环体
int i = 0;
do
{
if (children[i] != transform)
{
GameObject.Destroy(children[i].gameObject);
}
i++;
}
while(i<children.Length);