Toggle array of lights

I’m trying to enable an array of lights with a gui toggle. I was able to get it to work with a single light by using

Lights = GameObject.find("name")
Lights.light.enabled = LightsOn;

but ‘light’ isn’t a property of the array Lights, neither is enabled, that’s the error i’m getting.

bool lightsOn = false;
GameObject[] Lights;
Lights = GameObject.FindGameObjectsWithTag("Lights");

lightsOn = GUILayout.Toggle(lightsOn, "Lights");	
for(int i = 0; i < Lights.Length; i++)
{
	Lights.enabled = lightsOn;			
}

Thanks.

Lights[i].light.enabled = lightsOn;

The script you’re creating should be placed on the LightSource/GameObject

Then to turn them off/on you can use the following:

Add this to the top of the script:

var On = true;

Below is the code to be placed in the update function:

if(Input.GetButtonDown("Fire2")){
	if(On == true){
		light.enabled = false;
		On = false;
	}
else 
	{
		light.enabled = true;
		On = true;
	}
}

Now you should be able to toggle the lights by pressing the RMB.

I’m not exactly sure what it is you’re trying to achieve though, but reading through your code I can notice a few contradictions. Such as “Lights” is being associated with a tag and not an actual object so it does this in sequence not altogether. Also there are a few lines of code that don’t really make any sense such as line 2.

Konkor4: Thanks for your post! When I started scripting unity I was having trouble accessing scripts on other GameObjects. So I got in the habit of trying to control everything from a central script. I’ve actually been wondering what the best way to organize all of this was. I’m creating interactive architectural walkthroughs, in this case, you can turn lights on and off . My main ‘UnerInterface’ is getting out of hand, and if one piece of code doesn’t work, the entire thing doesn’t work. I was also thinking it would be hard to keep tract of scripts on each GameObject. What is the best way to organize and keep tract of scripts?

Your question about line 2, 1st code block or 2nd? 1st ‘LightsOn’ is a bool. 2nd defining an array. I usually stick with the first thing I get to work, it’s often not the best approach.

In your code, why are you using Var instead of Bool? And thanks again!

this works