I want get gameobjects except destory obejct in loop.

I want get gameobjects except destroy object.

My code is here. (It is just sample.)

> void free(){
>     GameObject[] all;
> 
>     while(true){
>         all = GameObject.FindGameObjectsWithTag("test");
>         destroy(all[Random.Range(0,5)]);
>     }
> 
> }

First in loop. array all is has object count 10 (It is not mean array size);
and I was destroy one object in array. So Second time i was expect count 9.
But array has still count 10;

Perhaps, you are looking for this:

using System.Collections.Generic;

List<GameObject> all;

void Start()
{
    all = GameObject.FindGameObjectsWithTag("test");
}

void free(){
    while(all.Count > 5){   // Adjust this condition per your need, it was running infinitely.
       GameObject gObj = all[Random.Range(0,5)]; 
       all.Remove(gObj);
       destroy(gObj);
    }
}