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;
}
ok cool i understand that part, thank you for that help. but now how would i tell this script to only activate if the object is present in the scene?
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.
I'm not sure i need a loop for what i want to do. I just want to disable all the lights that's all. Instead of using LightSources[0].GetComponent.<Light>().enabled = false; i want to use a line that will disable them all at once. Since i can't use LightSources[0, LightSources.length].GetComponent.<Light>().enabled = false; there must be something similar.
ok cool i understand that part, thank you for that help. but now how would i tell this script to only activate if the object is present in the scene?
– radar089