What I am trying to do is use one Switch to turn multiple Light2d Using GameObject[ ]. Well It works but I get a Null Reference Object reference not set to an instance of object. The lights still get turned on even if they are not in the public array and that is not what I want. What I need is that one I jump on a switch it activates a bunch of lights (Light2D) that are in the public array with out the errors. Here is the code that I am using to try to accomplish this. I have tried so much and nothing works.
public class LightsOn : MonoBehaviour
{
public GameObject lSwitch;
public GameObject[] lights;
void Start()
{
lights = GameObject.FindGameObjectsWithTag("LightToActivate");
}
void Update()
{
LightIsOn();
}
public void LightIsOn()
{
if (lSwitch.GetComponent<SwitchActivate>().isOn == true)
{
foreach (GameObject lights in lights)
{
lights.GetComponent<UnityEngine.Experimental.Rendering.LWRP.Light2D>().intensity = 1;
}
}
}
}