- Home /
How to Spawn one object at a time in Transform Array
Hi Everyone,
This is my first question so I apologise if I miss something off.
numOfEnemies = population / 100000;
int enemySpawnIndex = Random.Range(0, enemySpawn.Length);
for (int i = 0; i < numOfEnemies; i ++)
{
Instantiate (enemy, enemySpawn[enemySpawnIndex].position, enemySpawn[enemySpawnIndex].rotation);
totalNumOfEnemies++;
}
Above code creates Min Enemies 1 and Max 9, but my problem is when I have more than 1 Enemy, it spawns them in the same spawn location as opposed to spawning one enemy in each slot in my Transform array. I'm kind of new to programming and I can't figure out how to set it so if 9 enemies spawn, they each have a different position.
Any help would be appreciated, thank you!
Well one issue is that you are setting the enemySpawnIndex one time so it's only going to choose that one time number.
Inside your for loop, before the Instantiate() put in enemySpawnIndex = Random.Range(0, enemySpawn.Length);
That should choose a different number every time. Try that and see how it works!
Answer by Jakey020120 · Jun 01, 2015 at 11:38 PM
You Sir, are a Gentleman!
That gets it semi working, only problem is some still spawn on top of each other. I'm half way there though, Thanks man!
Your answer
Follow this Question
Related Questions
Way Point Assignment 4 Answers
How to Spawn a Prefab over an Array of Transforms, One Prefab at a Time? 1 Answer
Unity3D - Playback object array of position (with dynamic velocity) 0 Answers
Getting radius around multiple transform 1 Answer
How to assign variables to each GameObject in an array 1 Answer