- Home /
How to instantiate the first 8 gameobjects in an array (UnityScript)?
Hi, I'm making a simple random generation script. Right now the script has 2 arrays: one for the objects it should spawn and another for the spawn locations. I've made it so that at the start of the level, the script shuffles the "objects to spawn" array, so the order is randomized. Then the script should spawn the objects (rooms) to the spawnpoints.
The thing I can't figure out is how to spawn (instantiate) just the first 8 gameobjects of the shuffled array. Right now the SpawnRooms function spawns random objects between 0 and 7 in the array until the spawn points are filled, but what I'd want for it to do is spawn the first 8 in the array (or element 0, 1, 2, 3, 4, 5, 6 and 7) The goal is to never spawn the same room twice, which would happen with shuffle array -> spawn the first 8 objects.
my code:
var objectsToSpawn = new GameObject[12];
var spawnLocations = new Transform[8];
function Start(){
Shuffle();
SpawnRooms();
}
function Shuffle() {
for (i = 0; i < objectsToSpawn.Length; i++) {
var temp = objectsToSpawn[i];
var randomIndex = Random.Range(0, objectsToSpawn.Length);
objectsToSpawn[i] = objectsToSpawn[randomIndex];
objectsToSpawn[randomIndex] = temp;
}
}
function SpawnRooms(){
for (var i=0; i < spawnLocations.length; i++){
var thingToSpawn : int = Random.Range( 0, 7);
Instantiate( objectsToSpawn[thingToSpawn], spawnLocations[i].position, transform.rotation );
}
}
Answer by robertbu · Oct 08, 2013 at 04:05 PM
Since you are shuffling the array, you don't need to generate random values in this funciton. Try:
function SpawnRooms(){
for (var i=0; i < spawnLocations.length; i++){
Instantiate( objectsToSpawn[i], spawnLocations[i].position, transform.rotation
}
}
Of course! I knew it was something this simple, I'm just not much of a programmer... Anyway, it works, so thank you!
Thanks much for this answer. Stumbled and bumbled my way around trying to do this all afternoon and finally found this thread.
Also a note the last line above should read
Instantiate( objectsToSpawn[i], spawnLocations[i].position, spawnLocations[i].rotation);
assu$$anonymous$$g you want the spawned object's rotation to match that of the selected spawnlocation
Since i Found this all so helpful one last addition to the SpawnRooms function -
function SpawnRooms(){
for (var i=0; i < spawnLocations.length; i++){
var spawnedobject = Instantiate( objectsToSpawn[i], spawnLocations[i].position, spawnLocations[i].rotation); // this line creates a new variable and sets it to the Instantiated object
spawnedobject.transform.parent = spawnLocations[i].transform; // this line then sets the parent of the Instantiated object to that of the SpawnLocation object
}
}
This way if one of your SpawnLocations is parented to some other object or even a bone in an animated rig - the spawned object will maintain the parenting relationship of the SpawnLocation. Nice !!!
Your answer
Follow this Question
Related Questions
Instantiate Prefab at random times but keep 3 from spawning 2 Answers
instantiate a random object from multiples via 'tag' 1 Answer
RandFuncs.Shuffle question 0 Answers
multiple object in multiple spawn Point with out repeat 0 Answers
Randomly instantiate objects from array without choosing the same item twice. 2 Answers