using System.Collections; using UnityEngine; public class Twinkler : MonoBehaviour { [SerializeField] float intensity_Min; [SerializeField] float intensity_Max; [SerializeField] float timeSpan = 1.0f; Light cLight; // Light component attached to the object also attached to this script Coroutine twinkleRoutine; // Coroutine toggler to prevent rapid blinking // Start is called before the first frame update void Start() { cLight = GetComponent<Light>(); // Gather component from object } // Update is called once per frame void Update() { // Stop twinkling if paused if (MainMenu.IsPaused) { if (twinkleRoutine != null) { StopCoroutine(twinkleRoutine); twinkleRoutine = null; } else return; } twinkleRoutine ??= StartCoroutine(Twinkle()); // Begin the coroutine } IEnumerator Twinkle() { while (true) { float intensityValue = Random.Range(intensity_Min, intensity_Max); if (intensityValue == cLight.intensity) continue; else { float t = 0.01f; timeSpan = Mathf.Max(0.1f, timeSpan); float dif = (intensityValue - cLight.intensity) * t; while (t <= 1) { cLight.intensity = Mathf.Clamp(cLight.intensity, intensity_Min, intensity_Max); cLight.intensity += dif; t += 0.01f; yield return null; } } yield return null; } } }