x


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!

more ▼

asked Oct 18, 2011 at 12:40 AM

TripodGRANNE gravatar image

TripodGRANNE
101 33 23 26

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 ); } }

more ▼

answered Oct 18, 2011 at 12:51 AM

aldonaletto gravatar image

aldonaletto
57.2k 31 79 292

It works just as id like it to thanks a ton man!

Oct 18, 2011 at 02:08 AM TripodGRANNE
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2835
x2161
x2051
x1015
x768

asked: Oct 18, 2011 at 12:40 AM

Seen: 4092 times

Last Updated: Oct 18, 2011 at 02:08 AM