Enable scripts on a directional light

Hi there,

I have a script on a directional light to make it flash. What I want to do is to enable that script after a collision. Any help would be greatly appreciated.

Here is my code so far:

var flashinglight: GameObject;

function OnTriggerEnter(trigger: Collider){


I am getting this error too:

Assets/Scripts/LightingFlashingEnable.js(4,23): BCE0077: It is not possible to invoke an expression of type ‘UnityEngine.Light’.

A GameObject can have a light attached to it. If it does, that light can be accessed as an Object of type “Light” from the property called “light” on the GameObject. So, in your example:


will be an object of type Light. This has a property on it called enabled which defines whether or not the light is enabled or disabled. So to enable the light, one would code:

flashinglight.light.enabled = true;

In your code, you had coded


which basically tried to invoke a method called “light” that would be on the GameObject. However, “light” is a Light object on the GameObject and not a function.

I’m presuming that the script in the question is added to the thing that collides with the light?

In which case you need to be getting your script and just turning it on and off, now presuming it might one day collide with other things I suggest it look like this:

function OnTriggerEnter(trigger: Collider){
    var flashingLight = trigger.gameObject.GetComponent(LightFlashing);
    if(flashingLight != null)
         flashingLight.enable = true;


I presume your script in the comments is called LightFlashing for this to work, it can be simpler:

var minTime = .5;
var thresh = .5;

var enable: boolean = false; // boolean flag: enable trigger 1 when true

function Start() {
     light.enabled = false;//Or true if you want it on to start with

private var lastTime = 0;
function Update (){

if ((Time.time - lastTime) > minTime) {

    if (Random.value > thresh)

        light.enabled = true;


        light.enabled = false;

   lastTime = Time.time;

To get this to trigger there must be colliders and one of the objects must have a rigidbody (it can be isKinematic = true)

Now once activated your flashing light is going to keep flashing… You haven’t got code to turn it off there…