# Flickering Light Problem

**URL:** <https://discussions.unity.com/t/flickering-light-problem/416035>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [June 17, 2010, 9:35am UTC](https://discussions.unity.com/t/flickering-light-problem/416035 "2010-06-17T09:35:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grimmy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/grimmy/32/563438_2.png) [@grimmy](https://discussions.unity.com/u/grimmy)\
**Post date:** [June 17, 2010, 9:35am UTC](https://discussions.unity.com/t/flickering-light-problem/416035/1 "2010-06-17T09:35:24Z")

</div>

I want my light to flicker at a fixed rate but the following code makes the light turn on/off at random intervals…I really cant undertsand why???

```auto
function Update () {

	DoFlicker();
}

function DoFlicker()
{
	print("max intensity");
	myLight.intensity=maxIntensity;
	yield WaitForSeconds(flickerDelay*=Time.deltaTime);
	
	
	
	print("min intensity");
	myLight.intensity=minIntensity;	
	yield WaitForSeconds(flickerDelay*=Time.deltaTime);
	

}

```

---

<div class="post-metadata">

**Author:** ![grimmy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/grimmy/32/563438_2.png) [@grimmy](https://discussions.unity.com/u/grimmy)\
**Post date:** [June 17, 2010, 9:43am UTC](https://discussions.unity.com/t/flickering-light-problem/416035/2 "2010-06-17T09:43:28Z")

</div>

Hmm I just ried the following which works fine. So whats the difference??

```auto
function Start()
{
	DoFlicker();
}

function Update() {

}

function DoFlicker()
{
	//print("max intensity");
	while(true)
	{
	myLight.intensity=maxIntensity;
	yield WaitForSeconds(flickerDelay);

	//print("min intensity");
	myLight.intensity=minIntensity;	
	yield WaitForSeconds(flickerDelay);
	}

}

```

---

<div class="post-metadata">

**Author:** ![aubergine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aubergine/32/890563_2.png) [@aubergine](https://discussions.unity.com/u/aubergine)\
**Post date:** [June 17, 2010, 10:02am UTC](https://discussions.unity.com/t/flickering-light-problem/416035/3 "2010-06-17T10:02:21Z")

</div>

You can use this script:

```auto
using UnityEngine;
using System.Collections;

[AddComponentMenu("Tamer/General/lightFlicker")]
[RequireComponent(typeof(Light))]

public class lightFlicker : MonoBehaviour {

	public float interval = 0.7F;
	private Color col;
	private float flick = 0.0F;
	private LightShadows shadow;

	void Start () {
		col = light.color;
		shadow = light.shadows;
	}
	
	void Update () {
		if (Random.value >= interval) {
			flick = Random.value;
			light.color = new Color(col.r * flick, col.g * flick, col.b * flick);
			if (shadow != LightShadows.None) light.shadowStrength = flick;
		}
	}
}

```

If you are not on pro, just delete the shadow parts.
