- Home /
Changing the intensity of multiple point lights that act as porch lights
I created a simple porch light system to be used in my 2D top down RPG. They turn on after the "sunset" and turn off after the "sunrise". My first porch lights turns on and off great but the second "flickers" before turning off. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PorchLight : MonoBehaviour
{
public Light[] lights;
public float minTimeForLightsToTurnOn, maxTimeForLightsToTurnOn;
public float minTimeForLightsToTurnOff, maxTimeForLightsToTurnOff;
public float intensity;
public float rate;
void Update()
{
if(DayNightCycle.instance.dayLight.intensity == 0f)
{
StartCoroutine(TurnOnPorchLight());
}
if (DayNightCycle.instance.dayLight.intensity == DayNightCycle.instance.intensity)
{
StartCoroutine(TurnOffPorchLights());
}
}
public IEnumerator TurnOnPorchLight()
{
for(int i = 0; i < lights.Length; i++)
{
yield return new WaitForSeconds(Random.Range(minTimeForLightsToTurnOn, maxTimeForLightsToTurnOn));
lights[i].intensity = intensity;
}
}
public IEnumerator TurnOffPorchLights()
{
for (int i = 0; i < lights.Length; i++)
{
yield return new WaitForSeconds(Random.Range(minTimeForLightsToTurnOff, maxTimeForLightsToTurnOff));
lights[i].intensity = 0f;
}
}
}
I should add that this is not a problem when turn them off at them same time, which means no coroutines. But I would prefer to have them turn off at different times.
Comment
Answer by SlowCircuit · Jun 01, 2019 at 01:34 AM
I can't be sure from the code provided, but it's likely an issue of coroutines being called multiple times unintentionally. Add some Debug.Logs in them so you can see exactly when they're called and find out why it's happening.