Hi to all…
I have a problem in the void Update, when I press the key the sequence doesn’t start, I have to press the key two times… And then the sequence doesn’t stop when I press the key again… I think I made a mistake in IEnumerator StopLight()
Hope someone can Help me to solve my probem
public class LightAnimator : MonoBehaviour {
// number of seconds between changing light
public float lightTime = 1;
// the current light that is lit.
int currentLight = 0;
public Light[] lights;
public KeyCode controlKey = KeyCode.X;
private bool on = false;
IEnumerator ChangeLight()
{
yield return new WaitForSeconds(lightTime);
Debug.Log("Changing light to number "+currentLight);
// disable the current light
lights[currentLight].enabled = false;
// change the index to the next light
currentLight++;
// if the index is 4 or more, go back to the first light
if (currentLight >= 4)
currentLight = 0;
// the current light is now the next light, so enable it.
lights[currentLight].enabled = true;
// call this method again to create an infinite loop.
StartCoroutine("ChangeLight");
}
IEnumerator StopLight()
{
yield return new WaitForSeconds(lightTime);
// disable the current light
lights[currentLight].enabled = false;
// call this method again to create an infinite loop.
StartCoroutine("StopLight");
}
void Update(){
if (Input.GetKeyDown (controlKey)) {
on = !on;
if (!on) {
StartCoroutine ("ChangeLight");
}
if (on) {
StartCoroutine ("StopLight");
}
}
}
}