- Home /
Question by
wednes · Aug 06, 2015 at 09:22 AM ·
gameobjecttagcount
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;
Comment
@IgorAherne I used $$anonymous$$onoDevelop debugging function.
void free()
{
GameObject[] all;
all = GameObject.FindGameObjectsWithTag("test");
destroy(all[Random.Range(0,5)]);
}
would it not just be like this ?
Since the while loop is always true and continues to destroy object every iteration?
Im pretty sure it should be a for loop tbh.
Answer by NeverHopeless · Aug 06, 2015 at 11:45 AM
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611234628im_/https://answers.unity.com/themes/thub/images/avi.jpg)