- Home /
Blinking light 3 times after a pause of 15 seconds
Hi I am trying to blink point light 3 times after a pause of 15 seconds. I am unable to do it within a for loop as all statements get executed all at once. Can anyone suggest whats wrong?
public int totalDuration;
public int noOfFlashes;
public string typeOfFlash;
public Light flashLight;
IEnumerator flashLightForBuoys(){
for (int tempCounter = 0; tempCounter < 4; tempCounter++){
if (typeOfFlash == "small")
{
StartCoroutine(waitForInterval(1));
}
}
yield return new WaitForSeconds(15);
StartCoroutine(flashLightForBuoys());
}
private IEnumerator waitForInterval(int waitTime)
{
flashLight.enabled = true;
Debug.Log("flashLight turned on 1" + flashLight.enabled);
yield return new WaitForSeconds(waitTime);
flashLight.enabled = false;
Debug.Log("flashLight turned on 2" + flashLight.enabled);
}
private void Start()
{
StartCoroutine(flashLightForBuoys());
}
}
Comment
Best Answer
Answer by Hellium · Dec 12, 2018 at 12:37 PM
IEnumerator FlashLightForBuoys( int blinkCount = 1, float interval = 15, float onDuration = 1, float offDuration = 1 )
{
YieldInstruction onWait = onDuration < Mathf.Epsilon ? null : new WaitForSeconds(onDuration);
YieldInstruction offWait = offDuration < Mathf.Epsilon ? null : new WaitForSeconds(offDuration);
YieldInstruction intervalWait = interval < Mathf.Epsilon ? null : new WaitForSeconds(interval);
while( true )
{
for ( int blinkIndex = 1 ; blinkIndex <= blinkCount ; ++blinkIndex )
{
if (typeOfFlash == "small")
{
flashLight.enabled = true;
yield return onWait;
flashLight.enabled = false;
yield return offWait;
}
}
yield return intervalWait;
}
}
private void Start()
{
StartCoroutine(FlashLightForBuoys( 3, 15, 1, 1 ));
}
Your answer
Follow this Question
Related Questions
How to change the quantity of an IAP? 2 Answers
Unity 5 Resources.LoadAsync on Android slower than iOS? 0 Answers
Unity 5 mobile single stick control 6 Answers
Activity Indicator Centered 0 Answers