using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TrafficLightScript : MonoBehaviour
{
public GameObject greenLight;
public GameObject yellowLight;
public GameObject redLight;
public GameObject uiButton; // Reference to the UI Button
private bool isRedActive = false;
void Start()
{
// Initialize lights
greenLight.SetActive(true);
yellowLight.SetActive(false);
redLight.SetActive(false);
// Start the light switching coroutine
StartCoroutine(LightSwitcher());
}
IEnumerator LightSwitcher()
{
while (true)
{
// Green light phase
greenLight.SetActive(true);
yellowLight.SetActive(false);
redLight.SetActive(false);
isRedActive = false;
yield return new WaitForSeconds(10);
// Yellow light phase
greenLight.SetActive(false);
yellowLight.SetActive(true);
redLight.SetActive(false);
yield return new WaitForSeconds(2);
// Red light phase
greenLight.SetActive(false);
yellowLight.SetActive(false);
redLight.SetActive(true);
isRedActive = true;
yield return new WaitForSeconds(10);
}
}
void OnTriggerEnter(Collider other)
{
// Check if the train has the tag "Cube"
if (other.gameObject.tag == "Cube")
{
if (isRedActive)
{
// Display the UI button
uiButton.SetActive(true);
}
else
{
// Do nothing if green light is active
}
}
}
}