Hello everyone!
I have a traffic light, were is start its routine after the countdown finishes, I have held the process by a boolean, and I am calling it in an Update function, simply control the process by the boolean “playTrafficLights”, please check the code, its simlpe and clear.
Now my problem is when I need to trigger the lights (Start the loop/routine/process from beginning, no matter where is the previous one)
To explain more, I have a trigger(collider) close to the traffic lights, I will turn the red light on whenever the car gets close by, so OnTriggerEnter(from another script) , I am calling the function “TriggerTrafficLights()” ,which in turn changes the bool value.
First I taught , a second call to a function will stop the process/flow of the first call…
But as I tested , I have a mess in the colors since two parallel calls are having conflicts and even the order is in a mess.
can someone give me a hint of a technique of how to break the process of the lights(wherever it has reached execcution) and start a new one!
I am reading about coroutines, but still couldn’t find a solution!
Many thanks!
function Start() {
StartCountDown();
}
function Update() {
if(playTrafficLights)
TrafficLight();
}
function TrafficLight() {
playTrafficLights = false;
// red is on
yield WaitForSeconds (8);
// green is on
yield WaitForSeconds (8);
// yellow is on
yield WaitForSeconds (8);
playTrafficLights = true;
}
function TriggerTrafficLights() {
playTrafficLights = true;
}
function StartCountDown(){
// Do staff
// show countdown // do other staff
playTrafficLights = true;
}