turn spotlight on when entering a trigger

Hi, I’ve created a cube, deselected the Mesh Renderer and selected ‘is trigger’ and now I want a light to go on when my fpc enters this trigger. I’ve adapted a script I found somewhere that makes the light turn on when a certain button is pressed, but I want it to turn on when the trigger cube is entered. I’m a complete newbie with unity scripting, so it might be totally wrong, anyway it doesn’t work. Can somebody tell me what’s wrong? This is what my script looks like:

using UnityEngine;

public class FlashLight : MonoBehaviour

{

public Light FlashLightObject;

private bool LightEnabled = false;

void Update ()

{

function OnTriggerEnter (other : Collider) {

    LightEnabled = !LightEnabled;

    FlashLightObject.enabled = LightEnabled;

}

}
}

I’ve attached the script to my Spotlight, but in the Inspector of the spotlight, there is no variable beneath the script section, where I can attach my cube gameobject to as a trigger.

What you need to do is attach this script to the trigger itself and make a reference to the spotlight. Also, I’d recommend using public GameObject instead of public Light. Then you access the light component of that game object and switch it’s enabled state.

Something like this should work:

using UnityEngine;

public class LightTrigger : MonoBehaviour
{
	public Transform SpotLightObject;

	void OnTriggerEnter(Collider other)
	{
		SpotLightObject.light.enabled = !SpotLightObject.light.enabled;
	}
}

public GameObject point_light;//attach the point light here

 void Start()
     {
   point_light.active = false; //at start light will remain off
     }

  void OnTriggerEnter(Collider other) 
  {
	
	
 if(other.gameObject.name ==  "Cube") //here "Cube" put the name for the trigger enter 
	        {
         point_light.active = true;  
             }

     }

put the script in the first person controller above one is c# script and below one is java script and make the cube selected ‘is trigger’ if not working replay

    var point_light : GameObject;//attach the point light here

    function Start()
     {
   point_light.active = false; //at start light will remain off
      }

  function OnTriggerEnter (other : Collider) 
         {
	
	
 if(other.gameObject.name ==  "Cube") //here "Cube" put the name for the trigger enter 
	        {
         point_light.active = true;  
                     }

              }

Thanks a lot, it works fine!