- Home /
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!!
Answer by clunk47 · Nov 27, 2013 at 02:26 AM
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.
set waitTime
to the interval you want. In his example it's random between 3 to 6 seconds, but you can set it to any number you want.
I actually commented in the code.
//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);
Just change it to
waitTime = 60;
But if it's always going to be 60, just make it 60 at the variable dec at the top of the script:
....
public class FlickerLight : $$anonymous$$onoBehaviour
{
float waitTime = 60;
...
Then remove the waitTime declaration inside the coroutine, now the co would look like:
IEnumerator Flicker()
{
while(true)
{
//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);
}
}
You can do the same with offTime...
If this helps resolve your concern, please remember to Tick the answer as accepted.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Pause Game When Function Is Enabled? 3 Answers
Unity3D Game Time 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity3D Timer. 1 Answer