-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficLight.cs
executable file
·53 lines (48 loc) · 1.47 KB
/
TrafficLight.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrafficLight : MonoBehaviour {
public Transform red_light;
public Transform yellow_light;
public Transform green_light;
bool is_yellow = false;
// Use this for initialization
void Start () {
// Start light off red.
red_light.gameObject.SetActive(true);
yellow_light.gameObject.SetActive(false);
green_light.gameObject.SetActive(false);
}
// Actually turns red after three seconds
IEnumerator TurnYellow()
{
is_yellow = true;
yield return new WaitForSeconds(3f);
is_yellow = false;
red_light.gameObject.SetActive(true);
yellow_light.gameObject.SetActive(false);
green_light.gameObject.SetActive(false);
}
//Actually turns yellow immediately, then calls the
// turn yellow method to actually turn it red.
public void TurnRed()
{
StartCoroutine(TurnYellow());
red_light.gameObject.SetActive(false);
yellow_light.gameObject.SetActive(true);
green_light.gameObject.SetActive(false);
}
public void TurnGreen()
{
if (!is_yellow)
{
red_light.gameObject.SetActive(false);
yellow_light.gameObject.SetActive(false);
green_light.gameObject.SetActive(true);
}
else
{
Debug.Log("Tried to turn green while light was yellow");
}
}
}