Adding instantiated objects to arrays

Okay, so I have a script that spawns cubes. I attempted to create code that adds a new instantiated cube to an array when you collide with one of the spawned bodies. My script currently spawns the cube but does not add it to the array. Any help is appreciated, thanks.

Spawn Manager:

#pragma strict

var objectController : objectController;

function Start () {
var gameManager : GameObject;
gameManager = GameObject.Find("_gameManager");
objectController = gameManager.GetComponent("objectController");

function OnTriggerEnter () {

Cube Spawner:

#pragma strict

var spawnedBody : GameObject;
var radius : float = 20.0;
var numberOfSpawns : int = 20;
var spawnForever = false;

function Start () {
	if (!spawnForever) {
		for (var i : int = 0; i <= numberOfSpawns; i++) {
		Instantiate(spawnedBody, Random.insideUnitSphere * radius + Vector3(-15,15,-15), transform.rotation);

function Update () {
	if (spawnForever) {
	Instantiate(spawnedBody, Random.insideUnitSphere  * radius + Vector3(-15,15,-15), transform.rotation);


Object Controller:

#pragma strict

var bodyString : Transform[];
var prefab : Transform;
var position : int = 0;
var initalNumber : int = 5;

function Start () {

function initalSpawn (number : int) : IEnumerator {
	 for (var x = 0; x < number; x++) {
	 yield WaitForSeconds (.2);

function addObject () {
bodyString[position] = Instantiate(prefab, transform.position, transform.rotation);
  1. Check if the player has collided with a spawned body
  2. If so, spawn a new cube
  3. After the cube is spawned, add it to the array

spawnPoint on the CubeSpawner script is used as the position of the instantiate object. This was for test reasons; Obviously you’ll use your own Vector3.


#pragma strict

public static var cs : CubeSpawner;// Reference to the CubeSpawner script

public var cube : GameObject;// GameObject used to spawn
public var spawnPoint : Vector2;// Spawn position of the gameobject
public var cubeArray : Array;// Array holding all spawned cubes

// Used for initialization
function Start ()
	cs = this;// Set cs as the reference to the CubeSpawner script
	// Create the cubeArray
	cubeArray = Array ();

// Spawn a new cube
public function SpawnCube ()
	var newCube : GameObject;
	newCube = Instantiate ( cube, spawnPoint, transform.rotation );
	// Add the spawned cube to an array
	cubeArray.Push ( newCube );
	Debug.Log ( cubeArray );

spawnedBody on the CheckTrigger script should be the spawned bodies you spoke of in your game.


#pragma strict

// Check when a gameobject has entered the trigger
function OnTriggerEnter ( col : Collider ) 
	// If the collided object is a spawned body...
	if ( col.gameObject == spawnedBody )
		// ... spawn a new cube, add it to the array, and destroy the collided gameobject
		CubeSpawner.cs.SpawnCube ();
		//Destroy ( col.gameObject );

If you have multiple spawned bodies, there are 2 methods (I can think of now) that you can use.

Method #1:

Put all spawned bodies in an array. Inside of the trigger function, check through each gameobject in that array, and compare that gameobject to the collided gameobject. If they match, then spawn a new cube.

Method #2:

Give each spawned body a tag called ‘SpawnedBody’ (without quotes). Inside of the trigger function, compare the collided gameobjects tag to the spawned bodies tag. If they match, spawn a new cube. This method is cleaner and seems easier to do than method 1.

p.s. Do not use arrays, specially in Unityscript; Instead, use a List. Here are some great references for learning what list are and how to use them.