[Super Beginner Thread] How to enable and disable a "blinking" object with GetKeyDown

I’m in the process of trying to create a working traffic light where, if the light is green and the user presses the space bar, the yellow light will blink on and off until the space bar is pressed again which makes it switch to the red light. My only problem right now is that I’m not sure what function I should use in order to get the light to blink on and off by itself until it’s told to stop. I’ve had a little bit of success using the StartCoroutine function but it only blinks once and I have to keep pressing the space bar for it to keep blinking before it changes to another state.

    void StateBlinkYellow()
    {
        GameObject greenLight = GameObject.Find("Green Light");
        if (greenLight.GetComponent<Renderer>().material.color == Color.green && Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(BlinkTime());
        }
    }

    IEnumerator BlinkTime()
    {
        while (true)
        {
            GameObject yellowLight = GameObject.Find("Yellow Light");
            yellowLight.GetComponent<Renderer>().material.color = Color.yellow;
            yield return new WaitForSeconds(0.5f);
            yellowLight.GetComponent<Renderer>().material.color = Color.gray;
        }
    }

Try something like this:

    private GameObject greenLight, yellowLight;
    private bool blinkYellow = false;
    private Renderer ourRenderer;

    void Start()
    {
        greenLight = GameObject.Find("Green Light");
        yellowLight = GameObject.Find("Yellow Light");
        ourRenderer = yellowLight.GetComponent<Renderer>();
    }

    void Update()
    {
        if (!blinkYellow && Input.GetKeyDown(KeyCode.Space))
        {
            blinkYellow = true;
            StartCoroutine(BlinkTime());
        }
        else if(blinkYellow && Input.GetKeyDown(KeyCode.Space))
        {
            StopBlinkingYellow();
        }
    }

    void StopBlinkingYellow()
    {
        blinkYellow = false;
    }

    IEnumerator BlinkTime()
    {
        while (blinkYellow)
        {
            ourRenderer.material.color = Color.yellow;
            yield return new WaitForSeconds(0.5f);
            ourRenderer.material.color = Color.gray;
            yield return new WaitForSeconds(0.5f);
        }
        ourRenderer.material.color = Color.green;
    }

The fixed the blinking problem, I just have to make sure it blinks only when green now and that it goes straight to red when I stop the blinking. That helped me out a ton though, I was struggling with it for the longest time. If I’m interpreting this correctly, you created the boolean to control when the light started and stopped blinking and then in the IEnumerator you built it so that the color switches back and forth when the boolean is true and then returns to it’s normal color when it’s not true?

Right, it goes back to the green color when it breaks out of the while loop.

I didn’t consider anything more than what was in your existing code, so I didn’t put anything as far as making it red, or having other “cycles” it can go through, just copy, rename, then modify that coroutine to do stuff like “Green, wait 4 seconds, Yellow, wait 1.5 seconds, Red, wait 4 seconds…” and then run those depending on some kind of timer or whatever I suppose.

Awesome, I appreciate the help and quick responses. You’ve saved me from plenty of hours of frustration!

No problemo, good luck!