I'm trying to make operational turn signals/hazards for a vehicle I'm making. I'd like to use coroutines for this, but I don't seem to have a very good understanding of how coroutines work. I'm hoping someone can 1. help me get these turn signals working and 2 (and more importantly) help me gain a better understanding of coroutines. I've posted my code below. These need to be fully operational just like a real vehicle. I hit a button, they stay on until either I hit the button again, or until the steering goes far enough to the side and then back to center, only one can be on at a time, and hazards would override them as well. Thank you in advance for any help!
private void OperateTurnSignals()
{
if (GetControlValueAsBool("LeftTurnSignal") && !isCoroutineActive)
{
StopAllCoroutines();
TurnOffTurnSignals(RightTurnSignals);
isCoroutineActive = true;
StartCoroutine(TurnSignalTiming(LeftTurnSignals));
}
else if(GetControlValueAsBool("RightTurnSignal") && !isCoroutineActive)
{
StopAllCoroutines();
TurnOffTurnSignals(LeftTurnSignals);
isCoroutineActive = true;
StartCoroutine(TurnSignalTiming(RightTurnSignals));
}
else if(GetControlValueAsBool("Hazards") && !isCoroutineActive)
{
StopAllCoroutines();
isCoroutineActive = true;
StartCoroutine(Hazards(LeftTurnSignals, RightTurnSignals));
}
else
{
StopAllCoroutines();
isCoroutineActive = false;
TurnOffTurnSignals(LeftTurnSignals);
TurnOffTurnSignals(RightTurnSignals);
}
}
private IEnumerator TurnSignalTiming(List<Light> turnSignals)
{
TurnOnTurnSignals(turnSignals);
yield return new WaitForSeconds(1.0f);
TurnOffTurnSignals(turnSignals);
yield return new WaitForSeconds(1.0f);
isCoroutineActive = false;
Debug.Log("end of coroutine");
}
private void TurnOnTurnSignals(List<Light> turnSignals)
{
foreach(Light light in turnSignals)
{
light.gameObject.SetActive(true);
}
}
private void TurnOffTurnSignals(List<Light> turnSignals)
{
foreach(Light light in turnSignals)
{
light.gameObject.SetActive(false);
}
}
private IEnumerator Hazards(List<Light> leftTurnSignals, List<Light> rightTurnSignals)
{
TurnOnTurnSignals(leftTurnSignals);
TurnOnTurnSignals(rightTurnSignals);
yield return new WaitForSeconds(1.0f);
TurnOffTurnSignals(leftTurnSignals);
TurnOffTurnSignals(rightTurnSignals);
yield return new WaitForSeconds(1.0f);
}