Flashlight Blink?

Good Evening,

Currently I’m working with flashlights. In my scene, I have a basic First Person Controller with a spotlight parented to the player camera (Main Camera.) I was wondering if there is anyway to make the flashlight blink on and off rapidly once every 60 seconds (even intervals.) If anybody could help me out with this, I would seriously appreciate it!!

1 Answer

1

using UnityEngine;
using System.Collections;

public class FlickerLight : MonoBehaviour 
{
	float waitTime;
	float offTime;
	bool on = true;

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

	IEnumerator Flicker()
	{
		while(true)
		{
			//Change waitTime values as desired.
			//If you don't want random, just use an int: Example - Set at declaration: float waitTime = 60;
			waitTime = Random.Range (3, 6);

			//How long to disable light component.
			offTime = Random.Range (0.001f, 0.05f);
			light.enabled = false;
			yield return new WaitForSeconds(offTime);
			light.enabled = true;
			yield return new WaitForSeconds(waitTime);
		}
	}
}

Thank you so much for your help! Is this configured for 60 second intervals? You are awesome.

If this helps resolve your concern, please remember to Tick the answer as accepted.