One of your problems is that you would be making all of these changes in a single frame. That is what the Update() function amounts to each time it is called: a single frame. There are different approaches to spacing out a timed event, but one way might be this:
float lastTime = 0; // Keeps track of the last time the light was changed
bool lastState = false; // Keeps track of the last state the light was set to, on (true) or off (false)
void Update () {
if (lastTime + Random.Range(0.5f, 2f) < Time.realtimeSinceStartup)
{
if (lastState == false) // If the light was off...
{
light.range = 5; // Turn it on.
lastState = true;
}
else // If the light was on...
{
light.range = 0; //Turn it off.
lastState = false;
}
lastTime = Time.realtimeSinceStartup; // And update the last time the light was set
}
}
Note the Random.Range() function. It will generate a value between 0.5 and 2, which correspond to .5 seconds and 2 seconds. It serves as the interval for the flicker. This ought to be an easier way to get a random stutter to the light.
EDIT:
I just realized that you were looking for javascript. Sorry about that. Still the concept applies, and aside from the way the variables are declared, I don’t think too much of the code is different.
private var lastTime : float = 0; // Keeps track of the last time the light was changed
private var lastState : boolean = false; // Keeps track of the last state the light was set to, on (true) or off (false)
function Update () {
if (lastTime + Random.Range(0.5f, 2f) < Time.realtimeSinceStartup)
{
if (lastState == false) // If the light was off...
{
light.range = 5; // Turn it on.
lastState = true;
}
else // If the light was on...
{
light.range = 0; //Turn it off.
lastState = false;
}
lastTime = Time.realtimeSinceStartup; // And update the last time the light was set
}
}
var minOnTime : float = 1;
var maxOnTime : float = 2.5;
var minOffTime : float = 0.2;
var maxOffTime : float = 0.6;
// set this in the inspector- drag the actual mesh onto it!
var lampRenderer : MeshRenderer;
private var lightIntensity;
private var glowyTexture : Texture2D;
private var blackTexture : Texture2D;
function Start()
{
lightIntensity = light.intensity;
blackTexture = new Texture2D(2, 2);
var cols : Color[] = new Color[4];
for( var i = 0; i < cols.Length; ++i ) {
cols *= Color.clear;*
*}*
*blackTexture.SetPixels(cols);*
*blackTexture.Apply();*
*glowyTexture = lampRenderer.material.GetTexture("_Illum") as Texture2D;*
*StartCoroutine(FlickerLoop());*
*}*
*function FlickerLoop()*
*{*
*while(true)*
*{*
*yield WaitForSeconds(Random.Range(minOnTime, maxOnTime));*
*light.intensity = 0;*
*lampRenderer.material.SetTexture("_Illum", blackTexture);*
*yield WaitForSeconds(Random.Range(minOffTime, maxOffTime));*
*light.intensity = lightIntensity;*
*lampRenderer.material.SetTexture("_Illum", glowyTexture);*
*}*
*}*
*```*
*<p>This will make the light randomly flicker by the four values you can change in the inspector. You can also adapt this script to make the light jump around a bit, or make it change colour!</p>*
*<p>(I'm not too hot with JS, since I'm usually a C# man, so tell me if this has any errors since it's not tested!)</p>*
*<p><strong>EDIT:</strong> There was one other issue- it should have been Color.clear, not Color.black where I set the 'black' texture! Sorry again (forgot how illum textures worked)</p>*