How to make a light flicker

is there a code i can use to make a light flicker on and off quickly

Try this script. Place it on the light gameobject.

It has two public variables that you can set in the inspector, timeOn and timeOff. They allow you to specify different amounts of time for the light to be on and off. The default is 1/10th of a second on, followed by half a second off.

var timeOn = 0.1;
var timeOff = 0.5;
private var changeTime = 0;

void Update() {
    if (Time.time > changeTime) {
        light.enabled = !light.enabled;
        if (light.enabled) {
            changeTime = Time.time + timeOn;
        } else {
            changeTime = Time.time + timeOff;
        }
    }
}

try this one if you want to make something like an eye to close and open it: http://unity3d.com/support/documentation/ScriptReference/Light-spotAngle.html put the minangle at 0 and the maxangle how you want and also the interval make it faster or slower.

i use this....applied to your directional light.

function Update () 
    {
        if ( Random.value > 0.9 ) //a random chance
        {
           if ( light.enabled == true ) //if the light is on...
           {
             light.enabled = false; //turn it off
           }
           else
           {
             light.enabled = true; //turn it on
           }
        }
    }

Like this

var flickerSpeed : float = 0.1

while (true){

Light.enabled = true;
Yield WaitForSeconds (flickerSpeed);
}
Light.enabled = false;
Yield WaitForSeconds (flickerSpeed);

If you want something you can tweak a bit more, maybe you can try this solution I came up with to make a cheap lightning effect. You can tweak the flickering level by changing the size of the array (smaller means more flickering, larger means a smoother variation). This may not be optimal but does a fine job.

using UnityEngine;
using System.Collections;

public class Ambiance : MonoBehaviour {

	// Your light gameObject here.
	public Light light;

	// Array of random values for the intensity.
	private float[] smoothing = new float[20];

	void Start(){
		// Initialize the array.
		for(int i = 0 ; i < smoothing.Length ; i++){
			smoothing *= .0f;*
  •  }*
    
  • }*

  • void Update () {*

  •  float sum = .0f;*
    
  •  // Shift values in the table so that the new one is at the*
    
  •  // end and the older one is deleted.*
    
  •  for(int i = 1 ; i < smoothing.Length ; i++)*
    
  •  {*
    

_ smoothing[i-1] = smoothing*;_
_
sum+= smoothing[i-1];_
_
}*_

* // Add the new value at the end of the array.*
* smoothing[smoothing.Length -1] = Random.value;*
* sum+= smoothing[smoothing.Length -1];*

* // Compute the average of the array and assign it to the*
* // light intensity.*
* light.intensity = sum / smoothing.Length;*
* }*
}