- Home /
Question by
unity_RdSByC3z-S4uPg · Jan 29 at 10:25 AM ·
scripting problemcoroutineobject referencepoint light
Light Flickering Problem - Wrong objects get referenced by the script
Hi, everyone Im having trouble with one of my scripts i want to make Lights flicker at a random pace, and it works Fine for 3 or less lights, but as soon as i add more than 3 lights all of them start to flicker even tho they dont have the sript applied to them. I cant figure out why it also seems like its an area thing like in a certain area it works but if i move them away it stops.
here is the Code for reference
public class FlickeringLights : MonoBehaviour
{
public bool isflickering = false;
public float timeDelay;
void Update()
{
if (isflickering == false)
{
StartCoroutine(flickeringLights());
}
IEnumerator flickeringLights()
{
isflickering = true;
// turn the Light on
this.gameObject.GetComponent<Light>().enabled = true;
timeDelay = Random.Range(0.01f, 0.5f);
// wait random amount of Time
yield return new WaitForSeconds(timeDelay);
// turn Light off
this.gameObject.GetComponent<Light>().enabled = false;
// wait random amount of Time again
timeDelay = Random.Range(0.01f, 0.2f);
yield return new WaitForSeconds(timeDelay);
isflickering = false;
}
}
}`
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613052315im_/https://answers.unity.com/themes/thub/images/avi.jpg)