Question by
Clix4k · Nov 07, 2016 at 11:45 PM ·
getcomponent
Disabling Multiple light sources in 1 script.
I am trying to make a lightswitch script. I can make the script disable one light source but how do I disable multiple lights (That I can drag and drop) without adding tags.
Aids Code Here (DOESN'T WORK)
var Source1 : Light;
var Source2 : Light;
var Source3 : Light;
var TurnedOn : boolean = true;
function Start () {
Source1 = GetComponent.<Light>();
Source2 = GetComponent.<Light>();
Source3 = GetComponent.<Light>();
Source2.enabled = true;
Source1.enabled = true;
Source3.enabled = true;
TurnedOn = true;
}
function Update () {
if (Input.GetKeyDown(KeyCode.E) && TurnedOn == true) {
TurnedOn = false;
Source2.enabled = false;
Source1.enabled = false;
Source3.enabled = false;
}
if (Input.GetKeyDown(KeyCode.E) && TurnedOn == false) {
TurnedOn = true;
Source2.enabled = true;
Source1.enabled = true;
Source3.enabled = true;
}
}
Comment
Adding the lights to an Array and loop through them.
Answer by Filhanteraren · Nov 08, 2016 at 04:00 AM
The fixed code in C# using an array and foreach loop
public Light[] lights;
private bool TurnedOn = true;
public void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (TurnedOn)
{
TurnedOn = false;
foreach (Light obj in lights)
obj.enabled = false;
}
else if (!TurnedOn)
{
TurnedOn = true;
foreach (Light obj in lights)
obj.enabled = true;
}
}
}
How do you identify them separately so that when I make "Source1" for example not the same as "Source3". When I added multiple light sources(The Component) to the script they all were different but changed to the first one on this list as soon as I pressed play. They were all the same basically after running it.