I cant make a stoplight delay from green to yellow, then to red, when my boolean is off. How can I make it delay and actually get yellow to show?
At the moment the current code, just makes the light Green when I make the bool true. But when I set the bool to false, it returns immediately to red and never attempts yellow. Ive tried corutines and using WaitForSeconds and it does not work out for me. here is what I have so far.
public class TrafficSystem : MonoBehaviour
{
[SerializeField]
bool SwitchLights;
[SerializeField]
GameObject RedLight;
[SerializeField]
GameObject YellowLight;
[SerializeField]
GameObject GreenLight;
// Use this for initialization
void Start()
{
RedLight.SetActive(true);
YellowLight.SetActive(false);
GreenLight.SetActive(false);
}
// Update is called once per frame
void Update()
{
ChangeLights();
ToggleLights();
}
void ChangeLights()
{
if (SwitchLights == true)
{
RedLight.SetActive(false);
GreenLight.SetActive(true);
}
if (SwitchLights == false)
{
{
GreenLight.SetActive(false);
RedLight.SetActive(true);
}
}
}
void YellowDelay()
{
YellowLight.SetActive(true);
}
void ToggleLights()
{
if (Input.GetKeyDown(KeyCode.F))
{
if (SwitchLights)
{
//turn on the light
SwitchLights = !SwitchLights;
}
else
{
//turn off the light
SwitchLights = !SwitchLights;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to interrupt wait coroutine in a loop? 0 Answers
Fade out / fade in scene w/ loading screen 0 Answers
Unity MVC animation coroutine 0 Answers
Nav Mesh Question 0 Answers