Typecasting problem?

Hello!

I have GameObject named “EyeLight”. I’ve buried “EyeLight” deep in heiarchy of Game objects. How can I reach it to enable / disable?

Everything I’ve tried so far has been bunk -

Light o_EyeLight;

Void Start()
{
    o_EyeLight = GameObject.Find ("EyeLight");
}
    
public void EyeLight(bool eyelight)
	{
	Light myEyeLight = o_EyeLight;
	if (myEyeLight != null)
	{
		if (eyelight) {
			myEyeLight.enabled = true;
		} else {
			myEyeLight.enabled = false;
		}
	}
}

The code rips at type conversion. I’m not sure what I’m doing wrong.

This should work try the code below

    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

  1. GetComponent is used in EyeLight method which means it will get called as many times as EyeLight will be called
  2. OP defined o_EyeLight as Light but in answer it is defined as GameObject and a new variable is created for Light