traffic light help .....

hey guyzz… m creating a project for the childrens who r MR s … i wnat to teach them , hw to deal with city traffic …i had created a city for them … nw am not able to proceed further … m facing problem in makin traffic simulation… i had designed a traffic light … but m nt able to make traffic system work … plz help me …as m new to unity …

Hey guys. I’m creating a project for
the children who are MRs. I want to
teach them how to deal with city
traffic. I have created a city for
them, now I’m not able to proceed
further. I’m facing problems in
making traffic simulation. I had to
design a traffic light, but I’m not
able to make the traffic system work.
Please help me as I’m new to Unity.

Well this is a very old thread, but someone else revived it so lets face the problem. I guess the OP has solved the problem already (or doesn’t need the answer anymore) but since the question is still open I’ve created a “simple” approach.

One way would be to represent each state of a traffic light by a gameobject. This could be the entire model with different textures in each state or lights atached at the right spots with the right color.

I would create one generic script for each traffic light (so no matter if it has 1, 2 or 3 lights) and one script to control the whole system.

I’ve created a sample scene with the whole package included. The two scripts are really simple and most things are managed directly in the scene. The actual sequence is created in the editor.

It simulates the sequence red --> red-yellow --> green --> yellow --> red
(that’s how the traffic lights works in germany :wink: )

If you can’t find the package link on the webplayer sample page above, here’s again the link to the package

This isn’t the best solution (if there is even one that can state it’s the best). It has some redundancies in the assets, but it’s quite simple and you can create any kind of traffic system without changing the code :wink: If this would be a serious project you could add a custom inspector to have a better editor for the sequence, but it works well with the default editor.

Note: The overview is just the whole system duplicated on a seperate layer and the lights turned upwards. The two systems run independently from each other but are in sync, so it’s just an eye candy :stuck_out_tongue:

try with this code

var verde : Light;
var amarillo : Light;
var rojo : Light;


function Start () 
{
	Semaforo();
}

function Update () 
{
	print(Time.time);
}

function Semaforo()
{	
	while(true)
	{
	Apagar();
	yield EncenderVerde();
	yield EncenderAmarillo();
	yield EncenderRojo();
	}
}
public function Apagar()
{
//verde.light.enabled=false;
amarillo.light.enabled=false;
rojo.light.enabled=false;
}

public function EncenderVerde()
{
	this.transform.name="semaforoenverde";
	verde.light.enabled=true;
	yield WaitForSeconds(10);
	verde.light.enabled=false;
}

public function EncenderAmarillo()
{
	this.transform.name="semaforoenamarillo";
	amarillo.light.enabled=true;
	yield WaitForSeconds(2.5);
	amarillo.light.enabled=false;
	yield WaitForSeconds(.5);
	amarillo.light.enabled=true;
	yield WaitForSeconds(.5);
	amarillo.light.enabled=false;
	yield WaitForSeconds(.5);
	amarillo.light.enabled=true;
	yield WaitForSeconds(.4);
	amarillo.light.enabled=false;
}

public function EncenderRojo()
{
	this.transform.name="semaforoenrojo";
	rojo.light.enabled=true;
	yield WaitForSeconds(10);
	rojo.light.enabled=false;
}

for me it worked only you should use two different sequences, using spot light