- Home /
Declare multiple transform locations in an 'Instantiate'
Hello, I am trying to have a random item spawn in multiple locations using one code (and not putting that one code on all the 23 different spawn locations). I have everything working in my code except it being able to spawn the random object in multiple locations. Here is my code for the Instantiate:
function spawnObject()
{
Instantiate (objectToSpawn[BuildingChoice], Block[1].position, Block[1].rotation);
}
The 'Block[1].position and the Block[1].rotation' is what I need help with. Can I replace the '1' with something so that it is the location of every object in my array?
Are you trying to randomly spawn to 23 different preset locations defined inside Block[], but only a handful at a time or something?
If so I would just do Random.range(0,22) inside a while loop. If you wanted to prevent duplicate placement then I would select from a list of values and pop out on selection.
while (isSpawning)
{
int num = Random.Range(0,22);
Instantiate(objectToSpawn[BuildingChoice], Block[num].position, Block[num].rotation);
}