- Home /
Question by
FatalNightmare · Mar 15, 2015 at 05:07 AM ·
instantiatearrayspawn
Multiple random spawns from an array
Hi, I am trying to have 36 different spawn points and each spawns a random item out of a list of 10. Right now, I have it so that the game randomly picks an item and has that one item spawn at all 36 spawns. How do I make it so that my Instantiate and array lists make a chance for a random item to spawn at all 36 spawns.
Code:
public var Block : Transform [];
public var objectToSpawn : GameObject [];
var TimeToSpawn : int;
var RandomNumber : int;
var BuildingChoice : int;
BuildingChoice = Random.Range(1.0, 10.0);
TimeToSpawn = Random.Range(1.0,3.0);
function Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Invoke ("spawnObject", TimeToSpawn); /
}
}
function Start ()
{
}
function spawnObject()
{
for(var n = 0; n < 36; n++)
{
Instantiate (objectToSpawn[BuildingChoice], Block[n].position, Block[n].rotation);
}
}
Comment
Best Answer
Answer by Eluate · Mar 15, 2015 at 05:21 AM
You need to put building choice in the for loop so that the item is picked randomly each time and not at the start once.
function spawnObject()
{
for(var n = 0; n < 36; n++)
{
BuildingChoice = Random.Range(1.0, 10.0);
Instantiate (objectToSpawn[BuildingChoice], Block[n].position, Block[n].rotation);
}
}
Your answer
Follow this Question
Related Questions
Instantiating prefab at child (spawnlocations are arrays) 2 Answers
add spawned GameObjects in an array C# 1 Answer
onTrigger spawn object C# 3 Answers
Spawn from array 2 Answers