Select every GameObject in GameObject Array

Dear UnityPro’s/more intelligent people than me,

I am experiencing some problems with my arrays, and I think it is my mistake of wrong defining the array/wrong handling of the array:

static var RallyPoint : GameObject[];
RallyPoint = new GameObject[50];

This is the array, now I am putting gameobjects into the array with another script:

GameSettings.RallyPoint[GameSettings.BuildingID] = Instantiate(RallyPointObject,GameSettings.BuildingIDs[GameSettings.BuildingID].transform.position,Quaternion.Euler(-90,0,0));

Works very well so far, though I don’t know if I did it the correct way(?)
Now in another script I want to let all the Gameobjects disappear from the scene:

for(var i : GameObject in GameSettings.RallyPoint)
	{
		i.renderer.enabled = false;
	}

And this is where I get an error/where it is not working.

NullReferenceException: Object
reference not set to an instance of an
object

As far as I get it the function can’t find a gameobject at the position of the array. But I clearly defined one above? So where is my mistake? Or did I define wrong?

I have the same problem

There are three optional solutions to the null reference that comes to mind,

#1.
If BuildingID never increments when you assign GameObjects to your array.

for (GameSettings.BuildingID = 0; i<GameSettings.RallyPoint.Length; GameSettings.BuildingID++) {
    GameSettings.RallyPoint[GameSettings.BuildingID] = Instantiate(RallyPointObject,GameSettings.BuildingIDs[GameSettings.BuildingID].transform.position,Quaternion.Euler(-90,0,0));
}

However I suspect that BuildingID is a number you work with in another way later in your scripts, as you would equally be able to just call GameSettings.RallyPoint.Length in this case. Use a temporary incrementing int in that case when assigning to the array.

#2.
If RallyPoint doesn’t assign all GameObjects at once, you then call for all positions in the built-in array. Either you can create an ArrayList which is resizable or rebuild your built-in array when you add or remove rally points. Choose arrays carefully.

#3.
The more obvious regarding the error message but not as probable, some or all of the objects does not have a renderer component.

if (i.GetComponent(Renderer)) i.renderer.enabled = false;