My traffic light script.

Hello, I made a traffic light script. It has only 2 lights, though. I just wanted to know if this is a good was of doing it or not. If not, can someone point me in a direction? thanks

var Red : Transform;
var Green : Transform;

function Start () {

Green.GameObject.enabled = false;
Red.GameObject.enabled = true;

}

function Update () {

if (Red.GameObject.enabled = true) {
 WaitForSeconds(30);
 Green.GameObject.enabled = false;
 
 }
 

else (Green.GameObject.enabled = true) {
 WaitForSeconds(30);
 Red.GameObject.enabled = false;
 
 }
 
}

I haven’t tested the code below, but something like this should work better. For one thing, you can’t put a yield statement inside Update so it has to be a coroutine. Update cycles through the code every frame and yield pauses code for a fixed amount of time, which would be a conflict and give you an error message. Plus, you enable and disable a light object, you set a game object to active or not active. I’m assuming you’re using point lights. You can just turn them on and off rather than inactivate the whole object. Give this a try and let me know if you have problems. :slight_smile:

var Red : Light;
var Green : Light;

private var switch : boolean = true;

function Start ()
{
ChangeLight();
}

function Update ()
{
Red.enabled = switch;
Green.enabled = !switch;
}

function ChangeLight()
{
while (true)
{
switch = !switch;
yield WaitForSeconds(30);
}
}

Better yet

var Red : Light;
var Green : Light;

private var switch : boolean = true;

function Start ()
{
ChangeLight();
}

function ChangeLight()
{
while (true)
{
switch = !switch;
Red.enabled = switch;
Green.enabled = !switch;
yield WaitForSeconds(30);
}
}

For both of those, I get an unexpected token : switch. :face_with_spiral_eyes:

Not sure why you’re getting the error. I’ll try the code later when I’m in front of my computer and see what’s wrong (unless someone else has a suggestion first). Sounds like the variable declaration is wrong. You might try declaring the switch variable inside the ChangeLight() function. This would make it a local variable, which should work fine.

Like this?

var Red : Light;
var Green : Light;

private var switch : boolean = true;

function Start ()
{
ChangeLight();
}

function ChangeLight()
{
while (true)
switch = !switch;
{
Red.enabled = switch;
Green.enabled = !switch;
yield WaitForSeconds(30);
}
}

That doesnt work. lol.

I think he meant like this:

var Red : Light;
var Green : Light;

function Start ()
{
    ChangeLight();
}

function ChangeLight()
{
    var switch : boolean = true;

    while (true)
    {
        switch = !switch;
        Red.enabled = switch;
        Green.enabled = !switch;
        yield WaitForSeconds(30);
    }
}

I know that booleans are declared in C# as “bool”. Is it supposed to be “boolean” in JS?

problem is that you can’t use switch as it is a reserved keyword. use _switch instead

Thanks ivkoni. I just realized that after trying the script in Unity. My bad. Anyway, here’s a simplified script that I tested and which works. Dumb error on my part. :slight_smile:

var Red : Light;
var Green : Light;

private var lightSwitch : boolean = true;

function Start ()
{
while (true)
{
lightSwitch = !lightSwitch;
Red.enabled = lightSwitch;
Green.enabled = !lightSwitch;
yield WaitForSeconds(30);
}
}

Thankyou! Now, how would I switch between 3 lights?(Green - Yellow - Red) ?

Once again, untested, but it seems pretty straight forward. Just replace the while loop and add a Yellow light variable.:slight_smile:

function Start()
{

Yellow.enabled = false;

while(true)
{
Green.enabled = true;
Red.enabled = false;
yield WaitForSeconds(30);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(4);
Red.enabled = true;
Yellow.enabled = false;
yield WaitForSeconds (30);
}
}

How can I make it so that there is only 1 light on at a time?

I tested the script and it only lights one at a time. Perhaps you left a remnant of the old script? Here’s the complete tested script:

var Red : Light;
var Green : Light;
var Yellow : Light;

function Start()
{

Yellow.enabled = false;

while(true)
{
Green.enabled = true;
Red.enabled = false;
yield WaitForSeconds(30);
Yellow.enabled = true;
Green.enabled = false;
yield WaitForSeconds(4);
Red.enabled = true;
Yellow.enabled = false;
yield WaitForSeconds (30);
}

}

Here it is traffic light updated added few staff.

//Modify by "pauloaguiar".
var Red : Light;
var Green : Light;
var Yellow : Light;
var GreenTimer : float = 5; // default 5sec.
var redTimer : float = 15; // default 15sec.
 
var estaVerde : boolean = true;
var estaVermelho : boolean = true;
 
function Start()
{
 Yellow.enabled = false;
 
 while(true)
 {  
  Verde();
  yield WaitForSeconds(GreenTimer);
  //--------------------------------------------------intermetent yellow light START-----------------------
  Yellow.enabled = true;  
  Green.enabled = false;
  yield WaitForSeconds(0.1);
 
  Yellow.enabled = false;  
  Green.enabled = false;
  yield WaitForSeconds(0.2);
 
  Yellow.enabled = true;  
  Green.enabled = false;
  yield WaitForSeconds(0.3);
 
  Yellow.enabled = false;  
  Green.enabled = false;
  yield WaitForSeconds(0.4);
 
  Yellow.enabled = true;  
  Green.enabled = false;
  yield WaitForSeconds(0.5);
 
  Yellow.enabled = false;  
  Green.enabled = false;
  yield WaitForSeconds(0.6);
 
  Yellow.enabled = true;  
  Green.enabled = false;
  yield WaitForSeconds(0.7);  
 
  Yellow.enabled = false;  
  Green.enabled = false;
  yield WaitForSeconds(0.8);  
 
  Yellow.enabled = true;  
  Green.enabled = false;
  yield WaitForSeconds(0.9);  
 
  Yellow.enabled = false;  
  Green.enabled = false;
  yield WaitForSeconds(0.10);
 
  Yellow.enabled = true;  
  Green.enabled = false;
  yield WaitForSeconds(0.1);
  //--------------------------------------------------intermetent yellow light END-----------------------
  Vermelho();
  yield WaitForSeconds (redTimer);
 }
}
// green and red lights function to call for others scripts if you need it;)
function Verde()
{
 if (estaVerde)
 {
  Green.enabled = true;    
  Red.enabled = false;
 }
}
function Vermelho()
{
 if (estaVermelho)
 {
  Red.enabled = true;  
  Yellow.enabled = false;
 }
}

This works whit 2 lights example. Alternative timers.

var GreenTimer : float = 0;
var GreencoolDown : float = 15.0;
var RedTimer : float = 0;
var RedcoolDown : float = 25.0;
//var yellowTimer : float = 0;
//var yellowcoolDown : float = 20.0;
function Start()
{
 Yellow.enabled = false;
}
function Update() 
{ 
 green();
 red();
}
function green()
{
 if (GreenTimer > 0)
 GreenTimer -= Time.deltaTime;
  
 if (GreenTimer < 0)
  GreenTimer = 0;
 
 if (GreenTimer == 0)
 {
  greenOn();
  GreenTimer = GreencoolDown;
 }
}
function red()
{
 if (RedTimer > 0)
 RedTimer -= Time.deltaTime;
  
 if (RedTimer < 0)
  RedTimer = 0;
 
 if (RedTimer == 0)
 {
  redOn();
  RedTimer = RedcoolDown;
 }
}
function redOn()
{
 Green.enabled = false;  
 Red.enabled = true;
}
function greenOn()
{
 Green.enabled = true;  
 Red.enabled = false;
}

The unity forums become very helpful when it’s a matter of traffic lights.

the code in java or c# ?? just asking… im begineer :wink:

all javascript.

By the way, Metbujang, don’t mistake Java for javascript. They’re not the same language.Just don’t want you spending money on Java books only to find out it’s unrelated.

I want to play too…

Here’s an cycle script for any length of stuff to cycle, add as many things as you want to thingsToCycle…

var thingsToEnable:GameObject[] ;
var delay:float = 1.0f;
private var position:int = 0;
	
function Start () {
	Cycle();
}

function Cycle(){
	while(true) {
		for (var i:int = 0; i < thingsToEnable.Length; i++) thingsToEnable[i].active = (i == position);
		yield WaitForSeconds(delay);
		position = thingsToEnable.Length - 1 > position ? position + 1 : 0;
	}
}

OK we will play…

Think intersection controller

//StreetLightController.js
import System.Collections.Generic;

enum VehicleTriggerType{
	main,
	mainTurn,
	side,
	sideTurn
}

enum LightCondition{
	green,
	yellow,
	red
}

class LightBox{
	var type : VehicleTriggerType;
	var green : GameObject;
	var yellow : GameObject;
	var red : GameObject;
}

var mainDuration : float= 20.0;
var mainTurnDuration : float= 10.0;
var sideDuration : float= 15.0;
var sideTurnDuration : float= 10.0;

var mainLights : LightBox[];
var mainTurnLights : LightBox[];
var sideLights : LightBox[];
var sideTurnLights : LightBox[];

private var main : GameObject;
private var mainTurn : GameObject;
private var side : GameObject;
private var sideTurn : GameObject;

function Start(){
	if(mainDuration < 8.0) mainDuration = 8.0;
	if(mainTurnDuration < 8.0) mainTurnDuration = 8.0;
	if(sideDuration < 8.0) sideDuration = 8.0;
	if(sideTurnDuration < 8.0) sideTurnDuration = 8.0;
	
	SetLights(mainLights, LightCondition.green);
	SetLights(mainTurnLights, LightCondition.red);
	SetLights(sideLights, LightCondition.red);
	SetLights(sideTurnLights, LightCondition.red);
	
	var condition : VehicleTriggerType = VehicleTriggerType.main;
	var start = Time.time;
	
	var mainTimer : float = 0.0;
	var mainTurnTimer : float = 0.0;
	var sideTimer : float = 0.0;
	var sideTurnTimer : float = 0.0;
	
	while(true){
		if(condition == VehicleTriggerType.main){
			mainTimer = Time.time - start;
			if(mainTimer > mainDuration - 5.0)
				if(!mainTurn  !side  !sideTurn) start = Time.time - mainDuration - 5.0;
			mainTimer = Time.time - start;
			if(mainTimer <= mainDuration - 5.0){
				SetLights(mainLights, LightCondition.green);
				SetLights(mainTurnLights, LightCondition.red);
				SetLights(sideLights, LightCondition.red);
				SetLights(sideTurnLights, LightCondition.red);
			}
			if(mainTimer > mainDuration - 5.0)
				SetLights(mainLights, LightCondition.yellow);
			if(mainTimer>mainDuration){
				if(mainTurn)condition = VehicleTriggerType.mainTurn;
				if(side) condition = VehicleTriggerType.side;
				if(sideTurn) condition = VehicleTriggerType.sideTurn;
				start = Time.time;
			}
		}
		if(condition == VehicleTriggerType.mainTurn){
			mainTurnTimer = Time.time - start;
			if(mainTurnTimer <= mainTurnDuration - 5.0){
				SetLights(mainLights, LightCondition.red);
				SetLights(mainTurnLights, LightCondition.green);
				SetLights(sideLights, LightCondition.red);
				SetLights(sideTurnLights, LightCondition.red);
			}
			if(mainTurnTimer > mainTurnDuration - 5.0)
				SetLights(mainTurnLights, LightCondition.yellow);
			if(mainTurnTimer > mainTurnDuration){
				condition = VehicleTriggerType.main;
				start = Time.time;
			}
		}
		if(condition == VehicleTriggerType.side){
			sideTimer = Time.time - start;
			if(sideTimer <= sideDuration - 5.0){
				SetLights(mainLights, LightCondition.red);
				SetLights(mainTurnLights, LightCondition.red);
				SetLights(sideLights, LightCondition.green);
				SetLights(sideTurnLights, LightCondition.red);
			}
			if(sideTimer > sideDuration - 5.0)
				SetLights(sideLights, LightCondition.yellow);
			if(sideTimer > sideDuration){
				condition = VehicleTriggerType.main;
				if(mainTurn)condition = VehicleTriggerType.mainTurn;
				start = Time.time;
			}
		}
		if(condition == VehicleTriggerType.sideTurn){
			sideTurnTimer = Time.time - start;
			if(sideTurnTimer <= sideTurnDuration - 5.0){
				SetLights(mainLights, LightCondition.red);
				SetLights(mainTurnLights, LightCondition.red);
				SetLights(sideLights, LightCondition.red);
				SetLights(sideTurnLights, LightCondition.green);
			}
			if(sideTurnTimer > sideTurnDuration - 5.0)
				SetLights(sideTurnLights, LightCondition.yellow);
			if(sideTurnTimer > sideTurnDuration){
				condition = VehicleTriggerType.side;
				start = Time.time;
			}
		}
		yield;
	}
}

function SetLights(lights : LightBox[], condition : LightCondition){
	for(var x : LightBox in lights){
		if(x.green)x.green.active = condition == LightCondition.green;
		if(x.yellow)x.yellow.active = condition == LightCondition.yellow;
		if(x.red)x.red.active = condition == LightCondition.red;
	}
}

function EnterTrigger(other : Collider, type : VehicleTriggerType){
	switch(type){
		case VehicleTriggerType.main :
			main = other.gameObject;
		case VehicleTriggerType.mainTurn :
			mainTurn = other.gameObject;
		case VehicleTriggerType.side :
			side = other.gameObject;
		case VehicleTriggerType.sideTurn :
			sideTurn = other.gameObject;
	}
}

function ExitTrigger(other : Collider, type : VehicleTriggerType){
	switch(type){
		case VehicleTriggerType.main :
			if(main == other.gameObject) main = null;
		case VehicleTriggerType.mainTurn :
			if(mainTurn == other.gameObject) mainTurn = null;
		case VehicleTriggerType.side :
			if(side == other.gameObject) side = null;
		case VehicleTriggerType.sideTurn :
			if(sideTurn == other.gameObject) sideTurn = null;
	}
}

Code for the blocks which tell if something is in the normal or turning lanes:

//VehicleTriggerController.js
var streetLightController : StreetLightController;
var triggerType : VehicleTriggerType = VehicleTriggerType.main;

function Start(){
	if(!collider)
		gameObject.AddComponent(BoxCollider);
	if(renderer)
		renderer.enabled = false;
	collider.isTrigger = true;
}

function OnTriggerEnter(other : Collider){
	if(other.gameObject.CompareTag("Vehicle"))
		streetLightController.EnterTrigger(other, triggerType);
}

function OnTriggerExit(other : Collider){
	if(other.gameObject.CompareTag("Vehicle"))
		streetLightController.ExitTrigger(other, triggerType);
}

Basically the lights stay green on the main strip until someone enters the mainTurn, side or sideTurn triggers. It then cycles to the area it is supposed to switch to, turns the appropriate lights on and allows traffic to go though. It then cycles to the next appropriate direction and cycles that. It will do a complete turn, main, turn side if all the triggers remain active.

Pros:
You can have multiple main triggers for each direction
You can have multiple lights for each direction.