GameObject o_EyeLight; //sets to GameObject so it can use the (GameObject.Find)
Void Start()
{
o_EyeLight = GameObject.Find ("EyeLight");
}
public void EyeLight(bool eyelight)
{
Light myEyeLight = o_EyeLight.GetComponent<Light>(); //look for the Light component
if (o_EyeLight != null) // now it's looking if it found an object not the light component
{
if (eyelight) {
myEyeLight.enabled = true;
} else {
myEyeLight.enabled = false;
}
}
}
Gameobject.Find returns a gameobject not a Light … try this in your Start method
Light o_EyeLight;
Void Start()
{
// Use GetComponent as least as you can as it increases the chances of GC (As said on a forum pos)
o_EyeLight = GameObject.Find ("EyeLight").GetComponent<Light>();
}
public void EyeLight(bool eyelight)
{
Light myEyeLight = o_EyeLight;
if (myEyeLight != null)
{
if (eyelight) {
myEyeLight.enabled = true;
} else {
myEyeLight.enabled = false;
}
}
}
Answer updated for future readers as OP found it incomplete
Selected answer is correct as well but with two problems
GetComponent is used in EyeLight method which means it will get called as many times as EyeLight will be called
OP defined o_EyeLight as Light but in answer it is defined as GameObject and a new variable is created for Light