Is there any way to instantiate an array of GameObjects at random places… here is my code i have the same object instantiating at 7 places is there any way to make it different objects at 7 places? Thanks a lot:
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject; // Array of different Enemies that are used.
var amountEnemies = 20; // Total number of enemies to spawn.
function Start()
{
Spawn();
}
function Spawn()
{
for (i=0; i<amountEnemies; i++) // How many enemies to instantiate total.
{
var pos: Transform = spawnPoints[i]; // Randomize the spawnPoints to instantiate enemy at next.
GameObject.Instantiate(enemyPrefabs, pos.position, pos.rotation);
}
}
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 7; // Total number of enemies to spawn.
private var useUniqueEnemies : boolean = true;
private var RandomEnemyPool;
private var RandomSpawnPool;
function Start()
{
var hasError_CantSpawn = false;
var i = 0;
if(amountEnemies < spawnPoints.length)
{
Debug.LogError("Too few Spawnpoints for Enemies to spawn!");
hasError_CantSpawn = true;
}
else
{
RandomSpawnPool = new Array();
for(i = 0; i < spawnPoints.length ; i++)
{
RandomSpawnPool.Add(i);
}
}
if(enemyPrefabs.length < amountEnemies)
{
Debug.Log("Fever Enemies than desired amount of enemies. Can't spawn unique enemies.");
useUniqueEnemies = false;
}
else
{
RandomEnemyPool = new Array();
for(i = 0; i < enemyPrefabs.length ; i++)
{
RandomEnemyPool.Add(i);
}
}
if(!hasError_CantSpawn)
Spawn();
}
function Spawn()
{
for (i=0; i<amountEnemies; i++) // How many enemies to instantiate total.
{
var c = Random.Range(0,RandomSpawnPool.length);
var pos: Transform = spawnPoints[RandomSpawnPool[c]]; // Randomize the spawnPoints to instantiate enemy at next.
RandomSpawnPool.RemoveAt(c); // Remove selected spot from list so we can't spawn there again
if(useUniqueEnemies)
{
c= Random.Range(0,RandomEnemyPool.length);
GameObject.Instantiate(enemyPrefabs[RandomEnemyPool[c]], pos.position, pos.rotation);
RandomEnemyPool.RemoveAt(c);
}
else
{
c = Random.Range(0,enemyPrefabs.length);
GameObject.Instantiate(enemyPrefabs[c], pos.position, pos.rotation);
}
}
}
Next thing to do is make sure that enemies/some manager tells us when, if ever, enemy is detroyed, what kind enemy it was and where it was spawned so we can add those numbers back to pool for future spawning. If that is needed.