Affect every object in array.

Hello. What I’m trying to do is disable all the lights i have selected in the Inspector, but since the number of lights may vary i can’t use: [0], [1], [2] etc. I guess i have to use [Lights.length] but i get the error “Array index is out of range.” when i run it maybe cause i haven’t set the min of the array. Here is a part of my script if that helps.

var LightSources : Light[];

function Update ()
{
LightSources[LightSources.length].GetComponent.<Light>().enabled = false;
}

Loop through the array like this:

foreach(Light light in LightSources) {
        light.enabled = false;
 }

or

 for(int i = 0; i < LightSources.Length; i++) {
      Light light = LightSources*;*

light.enabled = false;
}
As for your error: Note that you try to access the elements at position (LightSources.Length). If LightSources.Length is, say, 5, then you try to access the element. as position 5 However, with a Length of 5, the element positions are 0,1,2,3,4, so 5 is outside the scope of the array. In any way, this would only affect this one element in the array, not all of them as you might have thought.