- Home /
Instantiate an array of prefabs?
Hey there. So basically what I am trying to do is have multiple objects (ie, rock, tower, etc) randomly throughout my scene. Gameplay is as follows:
https://www.youtube.com/watch?v=mJ8NFJcs4Uw
Objects (obstacles) will hurdle towards you while you move your character onscreen left and right to dodge/avoid getting hit.
Ideally I'd like the system to be expandable so that I can add different obstacles down the line, through the editor, with as little programming as possible. As well as being able to define their spawn 'patterns' (time, size, etc).
Lastly, and while this isn't necessarily part of this question, if you'd be able to point me in the direction of having the instantiated objects, NOT spawn on top of each other, that would be super awesome!
Here is the code that I have thus far. What I was attempting to do was to create an 'obstacles' array, whereupon I would store all the objects that I'd like the game to spawn randomly. The Random spawning happens down on the Instantiate line. While this system works for a single game object (without using an array I mean) I simply can't wrap my head around multiple objects.
Thank you for any help given!! Also, I apologize for my incredible density, I am quite new, obviously, and am still learning all these things. I am probably using arrays totally wrong or something. :P
Here is a PasteBin link, since the UA code snippet is a little hard to read. (lemme know if this doesnt work)
TowerCreation Script
#pragma strict
/ Spawn & Reuse Objects - Originally created by 'save' (of Unity Answers) Updated: 2012-07-18 /
enum PLAYERAXIS { x,y,z }
var obstacles : GameObject[]; //Obstacles var obstacles1; var obstacles2; var obstacles3;
var playerTransform : Transform; //Player's transform (we use this to set position of the spawn area) var totalSpawnObjects : int = 50; //The amount of objects that is allowed in the scene var spawnPause : float = .2; //The pause between spawns var spawnAreaPosition : Vector3 = new Vector3(0,0,0); //The position of the spawn area var spawnAreaSize : Vector3 = new Vector3(200,0,50); //The size of the spawn area (we then randomly spawn inside this area in X, Y and Z) var followPlayerOnAxisDistance : PLAYERAXIS = PLAYERAXIS.z; //The axis the spawn area follow the player in distance var distanceFromPlayer : int = 200; //The distance of the spawn are to the player
private var spawnTimeScheduler : float = .0; private var spawnPointer : int = 0;
// Static variables for cached components of the objects below (we use this to access the spawned objects outside the script easily) static var objects : GameObject[]; static var objectsTransform : Transform[]; static var objectsRenderer : Renderer[];
function Start () { objects = new GameObject[totalSpawnObjects]; objectsTransform = new Transform[totalSpawnObjects]; objectsRenderer = new Renderer[totalSpawnObjects];
if (playerTransform==null) playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update () { if (Time.time>=spawnTimeScheduler) Spawn(); }
function Spawn () { //Position the spawn area spawnAreaPosition = playerTransform.position;
switch (followPlayerOnAxisDistance)
{
case PLAYERAXIS.x: spawnAreaPosition.x += distanceFromPlayer; break;
case PLAYERAXIS.y: spawnAreaPosition.y += distanceFromPlayer; break;
case PLAYERAXIS.z: spawnAreaPosition.z += distanceFromPlayer; break;
}
//Bake the spawn position into a variable
var spawnPosition : Vector3 = Vector3
(
spawnAreaPosition.x - Random.Range(-spawnAreaSize.x / 2, spawnAreaSize.x / 2),
spawnAreaPosition.y - Random.Range(-spawnAreaSize.y / 2, spawnAreaSize.y / 2),
spawnAreaPosition.z - Random.Range(-spawnAreaSize.z / 2, spawnAreaSize.z / 2)
);
//Spawn new object
if (objects[spawnPointer]!=null)
{
//Reuse the object
objectsTransform[spawnPointer].position = spawnPosition;
}
else
{
//Instantiate the object
var thisObject : GameObject = Instantiate(obstacles[0,1,2], spawnPosition, Quaternion.identity);
CurrentSpawnedComponentCache(spawnPointer, thisObject.gameObject, thisObject.transform, thisObject.renderer);
}
if (spawnPointer++ >= totalSpawnObjects - 1) spawnPointer = 0;
spawnTimeScheduler = Time.time + spawnPause;
}
//Cache the components of spawned objects function CurrentSpawnedComponentCache (i : int, g : GameObject, t : Transform, r : Renderer) { objects[i] = g; objectsTransform[i] = t; objectsRenderer[i] = r; }
//Draw the spawn area in Scene view function OnDrawGizmos () { Gizmos.color = Color.yellow; Gizmos.DrawWireCube (spawnAreaPosition, spawnAreaSize); }