- Home /
Spawning Objects Using An Array.
Hello, What id like to do is Instantiate an array of Prefabs to an array of Spawn points.
Say i have 5 prefabs in the objectsToSpawn Array, id like to spawn those prefabs at the array of spawnLocations.But say i have 6 spawnLocations i want to randomly select from my 5 objectsToSpawn and fill the 6 spawn points.
Whats happening is it randomly selects from the prefabs and spawns 1 object in a random spawn point from the spawnLocations Array.
Heres My Example Code:
//Objects To Spawn var objectsToSpawn : GameObject[];
//Spawn Locations var spawnLocations : Transform[];
function Spawn() { //Select From Objects To Spawn var thingToSpawn : int = Random.Range( 0, objectsToSpawn.length ); //Select From Spawn Locations var placeToSpawn : int = Random.Range( 0, spawnLocations.length ); Instantiate( objectsToSpawn[thingToSpawn], spawnLocations[placeToSpawn].position,transform.rotation); } Sorry i cant be more clear but i hope You can understand what i am trying to do. Thanks for the help!
Answer by aldonaletto · Oct 18, 2011 at 12:51 AM
You can have another function (or modify the one you have) to sweep all spawnLocations and create a random prefab at each one:
function SpawnAllPoints() { for (var i=0; i < spawnLocations.length; i++){ //Select From Objects To Spawn var thingToSpawn : int = Random.Range( 0, objectsToSpawn.length ); Instantiate( objectsToSpawn[thingToSpawn], spawnLocations[i].position, transform.rotation ); } }
Your answer
Follow this Question
Related Questions
How can I align instantiated prefabs randomly on runtime #C 0 Answers
add spawned GameObjects in an array C# 1 Answer
Random select from array and spawn 1 Answer
Creating a light in game C# 1 Answer