Need help with a specific light flickering script

Hello! Realizing I’m obviously not a programmer, I think I could need a bit of help getting a light flickering script more realistic and improved.

This is the code I’m currently using, mostly copied from some other forum posts about flickering lights, but after tweeking the parameters for a bit, I realize it need something more.

public Light ceilingLight;

float minFlickerSpeed = 0.08f;
float maxFlickerSpeed = 0.8f;

void Start ()
{
    StartCoroutine(LightFlickering());
}

IEnumerator LightFlickering() {

        ceilingLight.enabled = true;
        yield return new WaitForSeconds (Random.Range (minFlickerSpeed, 
        maxFlickerSpeed));

        ceilingLight.enabled = false;
        yield return new WaitForSeconds (Random.Range (minFlickerSpeed, 
        maxFlickerSpeed));

        StartCoroutine (LightFlickering());
}

What I’d like for it to do, would be that the light is mostly on and functioning like normal, but at semi-random intervals it flashes / blinks rapidly, and then continues functioning normally until the next interval. The reason for this behaviour is that I want the effect of flickering lights, but it can be quite noticeable and steal some focus in the scene if not done correctly.

Probably try this(Untested)

public Light ceilingLight;

    float minFlickerSpeed = 0.08f;
    float maxFlickerSpeed = 0.8f;
    float waitTime;

    void Start()
    {
        waitTime = Time.time + Random.Range(5, 15f);           //interval gap you wanna set(pass parameters according to your needs)
    }

    void Update()
    {
        if(Time.time > secondstoWait)
        StartCoroutine(LightFlickering());
    }

    IEnumerator LightFlickering()
    {

        secondstoWait = Time.time + Random.Range(5, 15f);
        ceilingLight.enabled = true;
        yield return new WaitForSeconds(Random.Range(minFlickerSpeed,
        maxFlickerSpeed));

        ceilingLight.enabled = false;
        yield return new WaitForSeconds(Random.Range(minFlickerSpeed,
        maxFlickerSpeed));

    }

Here is another implementation of light flickering that is straight-forward and provides a nice candlelight/campfire vibe.

using UnityEngine;

public class LightFlicker : MonoBehaviour
{
    #region Inspector-facing variables

    [Tooltip("Base intensity of the light, adjust as necessary in the inspector")]
    public float baseIntensity = 1f;

    [Tooltip("Range by which flicker varies (proportinal to base intensity)")]
    public float intensityVariance;

    [Tooltip("Speed (in seconds) in which intensity changes occur")]
    public float flickerSpeed;

    public bool stopFlickering = false;
    #endregion


    private Light _lightSource;


    public void Awake()
    {
        _lightSource = GetComponent<Light>();
        if (_lightSource == null)
        {
            Debug.LogError("Flicker script is not attached to a GameObject with a Light component");
            return;
        }
    }


    void Update()
    {
        if(!stopFlickering)
        {
            FlickerUsingPerlinNoise();
        }
    }


    private void FlickerUsingPerlinNoise()
    {
        float intensity = baseIntensity + (intensityVariance * baseIntensity) * Mathf.PerlinNoise(Time.time * flickerSpeed, 0f);

        // avoid going into negative intensity 
        if (intensity <= 0)
            intensity = baseIntensity * 0.01f;

        _lightSource.intensity = intensity;
    }

}

This script is easy to use since you can just set the intensity via the inspector (on this script component, the actual light intensity will be controlled by the script) during play back mode and then tune the variance (think of it as the percent of the base intensity you’ll allow the magnitude of the intensity shifting to reach) and speed of the effect (try a fast value first, e.g. 5, then pull back to get it just right). Then just right click and save the values before leaving play mode and paste them back in.


Hope this helps others!