How Do I Add An Instantiated Object To An Array?

I’m working on a game where cubes spawn randomly from 5 different spawn points, and I need to know how to get them into an array when spawned so that I can keep track of where the cubes spawned, that way I can check later to see if cubes of a certain color are in a row (bonus points if you can help me figure that out as well). The cubes don’t currently have colors assigned to them, but they well when this is done. Right now I’m just trying to get them into the array so I can keep track of them.

Here’s what I’ve got so far:

#pragma strict
var timer : float = 0.0;
var spawning : boolean = false;
var prefab : Rigidbody;
var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : Transform;
var spawn4 : Transform;
var spawn5 : Transform;
var cubes : GameObject[];
 
function Update () {
 //check if spawning at the moment, if not add to timer
	 if(!spawning){
	  	timer += Time.deltaTime;
	 }
 //when timer reaches 2 seconds, call Spawn function
	 if(timer >= 2){
	  	Spawn();
	 }
}
 
function Spawn(){
	 //set spawning to true, to stop timer counting in the Update function
	 spawning = true;
	 //reset the timer to 0 so process can start over
	 timer = 0;
	 
	 //select a random number, inside a maths function absolute command to ensure it is a whole number
	 var randomPick : int = Mathf.Abs(Random.Range(1,6));
	 
	 //create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
	 var location : Transform;
	 
	 //check what randomPick is, and select one of the 3 locations, based on that number
	 if(randomPick == 1){
		  location = spawn1;
		  Debug.Log("Chose pos 1");
	 }
	 else if(randomPick == 2){
		  location = spawn2;
		  Debug.Log("Chose pos 2");
	 }
	 else if(randomPick == 3){
		  location = spawn3;
		  Debug.Log("Chose pos 3");
	 }
	  else if(randomPick == 4){
		  location = spawn4;
		  Debug.Log("Chose pos 4");
	 }
	  else if(randomPick == 5){
		  location = spawn5;
		  Debug.Log("Chose pos 5");
	 }
	 
	 //create the object at point of the location variable
	 var thingToMake : Rigidbody = Instantiate(prefab, location.position, location.rotation);
	 // thingToMake.AddForce(Vector3(0,0,100));
	 
	 //halt script for 1 second before returning to the start of the process
	 yield WaitForSeconds(1);
	 //set spawning back to false so timer may start again
	 spawning = false;
}

Use .NET generic lists. Lots of posts on Lists. Here is a link that includes some information on many of the collection types used in Unity:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

At the top of the file you will need:

import System.Collections.Generic;

Then you will declare your variable:

var cubes : List.<GameObject> = new List.<GameObject>();

Then you can add them to the list with:

myList.Add(thingToMake.gameObject);

Note if you tag all of your cubes, you can use GameObject.FindGameObjectsWithTag() to get a current array of game objects with that tag. It is a bit less efficient than maintaining your own list, but it is fast enough to use every frame for any reasonable number of cubes.

Thanks for the help everyone! I actually ended up doing it this way:

#pragma strict
var timer : float = 0.0;
var spawning : boolean = false;
var prefab : GameObject;
var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : Transform;
var spawn4 : Transform;
var spawn5 : Transform;
//var cubes : GameObject[];
var board : GameObject[, ];
var boxColor : Color[];
private var count : int = 0;
private var color : int;
function Start()
{
	//cubes = new GameObject[40];//[max no of cubes]
	board = new GameObject[5,8];//[x,y]
	count =0;//just making sure this starts at zero
}
function Update () {
 //check if spawning at the moment, if not add to timer
	 if(!spawning){
	  	timer += Time.deltaTime;
	 }
 //when timer reaches 2 seconds, call Spawn function
	 if(timer >= 2){
	  	Spawn();
	 }
}
 
function Spawn(){
	 //set spawning to true, to stop timer counting in the Update function
	 spawning = true;
	 //reset the timer to 0 so process can start over
	 timer = 0;
	 
	 //select a random number, inside a maths function absolute command to ensure it is a whole number
	 var randomPick : int = Mathf.Abs(Random.Range(1,6));
	 
	 //create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
	 var location : Transform;
	 
	 //check what randomPick is, and select one of the 3 locations, based on that number
	 if(randomPick == 1){
		  location = spawn1;
		  Debug.Log("Chose pos 1");
	 }
	 else if(randomPick == 2){
		  location = spawn2;
		  Debug.Log("Chose pos 2");
	 }
	 else if(randomPick == 3){
		  location = spawn3;
		  Debug.Log("Chose pos 3");
	 }
	  else if(randomPick == 4){
		  location = spawn4;
		  Debug.Log("Chose pos 4");
	 }
	  else if(randomPick == 5){
		  location = spawn5;
		  Debug.Log("Chose pos 5");
	 }
	 
	 //create the object at point of the location variable
	 //thingToMake is just a temporary variable to hold our cube 
	 var thingToMake : GameObject = Instantiate(prefab, location.position, location.rotation);
	 thingToMake.renderer.material.color = boxColor;
	 color = Mathf.Abs(Random.Range(0, boxColor.Length));
	 //put elements in arrays
	 //store cubes in "max" and this will allow us to run checks against their color or any other property
	 //cubes[count] = thingToMake;
	 for(var i= 0; i < 8; i++)
	 {
	 	if(board[randomPick-1,i]==null)
	 	{
			 //add the cube to our "game board" to keep track of where it is, this is our 2D array
			 board[(randomPick-1),i] = thingToMake;
			 break;
		}
	 }
	 if(board[0,7] != null || board [1,7] != null || board [2,7] != null || board [3,7] != null || board[4,7] != null )
	 	Application.LoadLevel("LoseScene");
	 //halt script for 1 second before returning to the start of the process
	 yield WaitForSeconds(1);
	 //set spawning back to false so timer may start again
	 spawning = false;
	 //increment count
	 count++;
}

Adding to a UnityScript array is as simple as:

cubes.push(OBJECT);