Question by
tinylord202 · Mar 14, 2018 at 02:05 AM ·
listsarray of gameobjectsspawning-enemies
Convert a list into a GameObject[] Array,How to convert a list to a gameObject[]
I am trying to make a spawn system for a zombie game I am making. Since I plan to have the zombies spawn only in close locations I have found all objects with spawn tag and put them in a list. Then I check to see if the spawn is in range. I then want to put that into an array for processing in another script.
The control.spawn is my Game Control Array that I plan to use for spawning and my only problem is converting the list of zSpawn into an Array of Game Objects. ,I'm trying to get all of some gameobjects, and the way I found them was by using list. However, when I want to use the objects they are in a list and I don't know how to convert a list into an array(GameObject[]).Blockquote public GameControl control;
public float spawnDist = 10;
List GameObject> zSpawn = new List GameObject> ();
.
void Update (){
foreach (var obj in Zspawn){
if(Vector3.Distance(transform.position, obj.transform.position) < spawnDist)
control.spawn = zSpawn; }
}
}
Comment
Best Answer
Answer by exzizt · Mar 14, 2018 at 02:42 AM
Try:
List<GameObject> listOfGameObjects = new List<GameObject>();
GameObject[] arrayOfGameObjects = listOfGameObjects.ToArray();
You will need to use "System.Linq". Or, there's a manual way to do this.