How to Create a Traffic Light in Unity

This tutorial will guide you through the process of creating a simple traffic light system in Unity using C#. You’ll learn how to set up the traffic lights, add logic to control them, and simulate a working traffic light system.

Prerequisites

  • Basic knowledge of the Unity interface
  • Basic understanding of C# programming
  • Unity installed on your computer (any recent version)

Step 1: Setting up the Scene

First, we need to set up the scene in Unity with three lights (Red, Yellow, and Green) to simulate a traffic light system.

  1. Open Unity and create a new project.
  2. In the Hierarchy window, create three 3D objects representing the traffic lights. You can use Spheres for each light. Rename them to RedLight, YellowLight, and GreenLight.
  3. Place them on top of each other in the Scene view to simulate a vertical traffic light. Adjust their position and scale as needed.
  4. To make them look like real traffic lights, assign different colors to each sphere:
    • Select RedLight and change its material color to red.
    • Select YellowLight and change its material color to yellow.
    • Select GreenLight and change its material color to green.

Step 2: Creating the Traffic Light Script

Now, we will write a script to control the traffic light behavior. This script will cycle through the red, yellow, and green lights at regular intervals.

  1. In the Assets folder, right-click and select Create > C# Script. Name it TrafficLightController.
  2. Open the script in your preferred code editor and write the following code:
using System.Collections;
using UnityEngine;

public class TrafficLightController : MonoBehaviour
{
    public GameObject redLight;
    public GameObject yellowLight;
    public GameObject greenLight;
    
    public float redLightDuration = 5f;
    public float yellowLightDuration = 2f;
    public float greenLightDuration = 5f;
    
    private void Start()
    {
        StartCoroutine(TrafficLightCycle());
    }
    
    IEnumerator TrafficLightCycle()
    {
        while (true)
        {
            // Red light on
            redLight.SetActive(true);
            yellowLight.SetActive(false);
            greenLight.SetActive(false);
            yield return new WaitForSeconds(redLightDuration);
            
            // Green light on
            redLight.SetActive(false);
            yellowLight.SetActive(false);
            greenLight.SetActive(true);
            yield return new WaitForSeconds(greenLightDuration);
            
            // Yellow light on
            redLight.SetActive(false);
            yellowLight.SetActive(true);
            greenLight.SetActive(false);
            yield return new WaitForSeconds(yellowLightDuration);
        }
    }
}

This script defines a simple cycle where the red, green, and yellow lights turn on and off in sequence.

Step 3: Assigning the Lights to the Script

Now that the script is ready, we need to link the light objects to the script so that it knows which lights to control.

  1. Select the Main Camera or create an empty GameObject in the scene to hold the script. Name it TrafficLightController.
  2. In the Inspector, click Add Component and attach the TrafficLightController script.
  3. Assign the traffic light spheres to the script:
    • Drag the RedLight object into the redLight field in the script.
    • Drag the YellowLight object into the yellowLight field.
    • Drag the GreenLight object into the greenLight field.

Step 4: Testing the Traffic Light System

Now, it's time to test the traffic light system and see if everything works as expected.

  1. Press the Play button at the top of the Unity editor.
  2. Observe the traffic light system in action. The red, green, and yellow lights should cycle based on the time intervals you defined in the script.

If everything is working, you should see the red light turning on for 5 seconds, followed by the green light for 5 seconds, and then the yellow light for 2 seconds, repeating in a loop.

Step 5: Customizing the Traffic Light

You can easily customize the traffic light system by changing the time intervals for each light. In the Inspector window, modify the redLightDuration, yellowLightDuration, and greenLightDuration fields to change how long each light stays on.

For example, you can make the red light stay on longer to simulate a busy intersection or adjust the yellow light duration for a quicker transition.

Conclusion

You've successfully created a simple traffic light system in Unity. By following this tutorial, you've learned how to control GameObjects with C# and implement a timed loop to simulate a real-world system. From here, you can expand on this project by adding more features like pedestrian lights, countdown timers, or more complex traffic rules.

Links
Unity 6