- Home /
Calling an object from Array/List
Hi. I currently have an Array / List that I want to use . i want to spawn a certain enemy at a certain location. So the Array/List is able to be edited in the inspector.
When the user increases the size and input the new Enemy and Spawn Location. I would like the enemy to spawn at the location. Sorry If this has been posted before but I am not too sure on how to do this.
public GameObject[] Enemies;
public Transform[] SpawnLocation;
int EnemiesIndex = Enemies.Length;
int Spawnpointindex = SpawnLocation.Length;
if (other.gameObject.tag=="Player")
{
for (int i = 0; i < EnemiesIndex && i < Spawnpointindex; i++)
{
Instantiate(Enemies[i], SpawnLocation[i]);
Debug.Log("Enemies is spawned");
}
}
This is a snippet of the code that I have currently. With the size of the Array/List changing, I am unsure on how to code this. Thanks in advance. Can someone also advise me on whether to use Array or List? I am quite confused about which to use as they are quite similar to me.
Answer by jim3878 · May 30, 2018 at 05:21 PM
if you want instance Enemies at certain location , use this
Instantiate(Enemies[i], SpawnLocation[i].position,Quaternion.identity);
because
Instantiate(Enemies[i], SpawnLocation[i]);
is mean spawn an enemy to be a child of SpawnLocation[i].
second ,Personally, list is better than array unless you want to use generic programming.