I’m trying to make a script for a light switch and it has a bit that looks like this:
public bool start_off = true;
public Light light0;
public Light light1;
public Light light2;
public Light light3;
public Light light4;
private Light[] lights;
void Start ()
{
if (light0 != null)
{
lights[0] = light0;
}
if (light1 != null)
{
lights[1] = light1;
}
if (light2 != null)
{
lights[2] = light2;
}
if (light3 != null)
{
lights[3] = light3;
}
if (light4 != null)
{
lights[4] = light4;
}
//shutting lights off, if they should be off by default
if(start_off == true)
{
for (int i = 0; i < lights.Length; i++)
{
if(lights *!= null)*
{
lights*.enabled = false;*
}
}
}
}
The line where it says lights[0] = light0 throws this error:
NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
SwitchLights.Start () (at Assets/Scripts/Switches/SwitchLights.cs:24)
How do I fix this?
The strange part is that light0 is not empty, but assigned in inspector via drag & drop (and i’d like to retain the possibility of dragging and dropping).