What am I doing wrong on my array?

i have a script that creates 10 gas stations. I need all 10 of those gas stations to be put into an array as they are created. I think I am close but I keep getting the error message index out of range.

#pragma strict
var gasStation : GameObject;
var gasStationArray : GameObject[];
var numberOfGasStationsToSpawn : int = 10;
static var numberOfGasStations : int = 0;
var test : int = 0;
function Start () 
{
}

function Update () 
{
	CreateGasStations(); // This function will be deleted in the final project. 
}

function CreateGasStations()
{
	// This is just for testing and setting up. Will be deleted later;
	if(numberOfGasStations < numberOfGasStationsToSpawn)
	{
		numberOfGasStations++;
		Instantiate (gasStation, Vector3(Random.Range(44.0,57.0), Random.Range(40.0,49.0), 0), Quaternion.identity);
		gasStationArray[numberOfGasStations] = GameObject.Find("Gas Station #" + numberOfGasStations);
	}
}

You have a few problems to address:

First you need to initialize the array. Initialization of this array should happen when the script starts. So add this line to your Start() function:

gasStationArray = new GameObject[10];

This will make your variable, gasStationArray, a new array that can hold up to 10 GameObjects.

Your next problem is you call CreateGasStations() in the Update() function. This will cause this function to be run once per frame, but you only want to do this once when the script starts. To fix this, simply call CreateGasStations() in your Start() function instead of Update(). But make sure to call it after you initialize the array. You also have to remove it from your Update() function. So your Start() function should look like this:

function Start()
{
    gasStationArray = new GameObject[10];
    CreateGasStations();
}

Your next problem is you use numberOfGasStations to select an index from your array. The issue you have here is that you increment numberOfGasStations before you select from the array. Array index start at 0. So for example, your array has 10 items, so the index of the array is 0 - 9. Any other number will give you an index out of bounds exception. To fix this move:

numberOfGasStations++;

below

 gasStationArray[numberOfGasStations] = GameObject.Find("Gas Station #" + numberOfGasStations);

The last thing I will point out is how you find the object you just created. Instead of using GameObject.find() after you create the new gas station, the Instantiate function will return the game object that was created. You can save the new object to a variable, then put it into the array. So this is how CreateGasStations() should look:

function CreateGasStations()
{
    if(numberOfGasStations < numberOfGasStationsToSpawn)
    {
        var newStation : GameObject = Instantiate (gasStation, Vector3(Random.Range(44.0,57.0), Random.Range(40.0,49.0), 0), Quaternion.identity);
        gasStationArray[numberOfGasStations] = newStation;
        numberOfGasStations++;
    }
}

Array indicies are starting with 0, not 1. (So the first index is 0). You increment numberOfGasStations and THEN add it to the array, so your first index is 1.

Just move the numberOfGasStations++ below the gasStationArray[…] line.

You haven’t initialized your gasStationArray to numberOfGasStationsToSpawn anywhere. It’s being initialized in the inspector, so it’s almost certainly wrong. Also, don’t put the CreateGasStations function call in Update, not even for testing. Update runs once every frame.

#pragma strict

private var stations : GameObject[] = new GameObject[10];
var prefab : GameObject;

function Start()
{
	for(var i : int = 0; i < stations.Length; i++)
	{
		stations *= Instantiate(prefab, transform.position, Quaternion.identity);*
  • }*
    }
    Of course you will need your position to actually instantiate each, this just shows how to create 10 instances of a prefab on Start.