- Home /
Problem with Arrays
Hey guys, I want to make a switch script, where when the player presses the switch the lights will turn on and off.But the complicated part is when you press the switch not just one , but more than one lights will turn off. I tried to make a variable of type GameObject like : `var lights : GameObject[];` and then on the Update function i wrote `lights[].light.enabled = false (or true);` But that wouldn't work of course.I thought of making a loop with an i for just 10 lights , for testing purposes so i did something like
`for(i=0;i<11 lights[i].light.enabled = false (or true) ;` But it said Array out of Index... How can I enable and disable all the GameObjects from my array?
Answer by totru · Dec 14, 2012 at 08:32 PM
If you don't know the number of lights at all, but set them on the editor, best practice is to use:
for (var light : GameObject in lights) {
// do anything with current light here
}
Answer by Itinerant · Dec 14, 2012 at 05:48 PM
You have 10 lights? Then you want i<10, as your lights are filling up lights[0] to lights[9].
yea i did that and the console output Aray out of Index.. I will post the code i scripted this far.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Is this the reason my game runs slow? 1 Answer
Why can't I pass an array reference to another array variable? 1 Answer
C# scriping help 0 Answers
animation.isPlaying always true and Animation stuck at time 0 0 Answers