How to turn light for x secound[SOLVED]

Hi for all the people here
I’m a student for computer science
I’m in the first year of university . so we learn until now
only java and a lot of boring math . :frowning:
I want to try to build in unity traffic light
I’m new in unity and with c# so its not easy for me
I design a traffic light
and success to programming when I click button " 1 "
and its turn the light
but now I want to programing it for when I clock button “1”
its will start loop
5 sec red light
1 sec yellow light
and after this 10 sec green light

I really applicate if some on can help with this

I fount the code StartCoroutine but its give me error
I start like this

{

public Light redLight;

public Light greenLigh;

public Light yellowLigh;

// Use this for initialization

void Start()

{
}
// Update is called once per frame

void Update()

{
if (Input.GetButton(“1”))

{
redLight.intensity = 50f;
}
}
}

thanks
Yossi

yep, Coroutine is the way to go.

void Start()
    {
        StartCoroutine(LightLoop());
    }

    IEnumerator LightLoop()
    {
        Debug.Log("Red light On");
        //code for the red light = On
        //yellow and green = off
        yield return new WaitForSeconds(5); //red will be on for 5 sec
        Debug.Log("Yellow light On");
        //code for the Yellow light = On
        //red and green = off
        yield return new WaitForSeconds(1); //yellow will be on for 1 sec
        Debug.Log("Green light On");
        //code for the Green light = On
        //yellow and red = off
        yield return new WaitForSeconds(10); //green will be on for 10 sec
    }
1 Like

its work well THanks :)))))

Glad you got a working solution:) If you find yourself asking questions on the forums in the future, please refer to this page for how to properly insert code: Using code tags properly - Unity Engine - Unity Discussions
It will make it so your code looks much nicer/easier to read. =)

1 Like

I’m new here so next time I will us it
Thanks again :slight_smile: