How do I spawn a random gameObject in multiple positions (not random positions).
var balls : GameObject[];
var spawnPos : Transform[];
var testNumber : int = 9;
function Start ()
{
Create();
}
function Create()
{
for(var i=0; i < testNumber; i++)
{
var obj : GameObject = balls[Random.Range(0, balls.Length)];
var pos : Transform = spawnPos[Random.Range(0, spawnPos.Length)];
Instantiate(obj, pos.position, pos.rotation);
}
}
Right now the code above is spawning in random positions which I dont want, I have a set 9 positions and Id just like to spawn a random gameObject in all 9 positions. Help greatly appreciated (its been a while, Im drawing a blank).
Your best bet would be to not use Ramdon.Range() as that is what has it spawn in a random position.
If you want to spawn in preset positions leave a game object in the position and then add it to this script, then when calling create just use each of those game objects as the position of where to spawn.
Or another way would be to have x,y,z positions in a text file, then while reading the text file spawn things at those positions (each time you get to some delimiter). Although the easiest way would be the above of setting “spawning points” and those game objects into the transform list you have.