Coroutine to turn on and off objects.

hello there,
I am using this Coroutine to make my object active and inactive again and again.But it isnt working.by default my gameobject is active.i am calling this in Update(),but its only make the object inactive not repeating.please tell what change i made to fix the issue.

IEnumeratorEyes()
{
gameObject.SetActive(false);
yield return new WaitForSeconds(2);
gameObject.SetActive(true);
}

edit: edited my answer to be a bit more clearer

You have two options,

void Start()
{
    StartCoroutine("Eyes");
}

IEnumerator Eyes()
{
    for (; ; )
    {
        gameObject.SetActive(false);
        yield return new WaitForSeconds(2);
        gameObject.SetActive(true);
        yield return new WaitForSeconds(2);
    }
}

Or instead of starting a coroutine, invoke a repeating method,

void Start()
{
    InvokeRepeating("Eyes", 0.0f, 2.0f);
}

void Eyes()
{
    // Set it to the inverse of what the current active state is
    gameObject.SetActive(!gameObject.activeSelf);
}

Do not call StartCoroutine or InvokeRepeating in the Update method, as these are basically infinite-ish.

1 Like

Sir i have tried this already. its just blink the eyes with slow and fast rate.But it doesnt put the inerval between the blinks.i want to put 2 or 3 seconds interaval between one blink.So my object blink eyes one time after 2 or 3 second.How i can put that interval?

Which one are you using? If you want a long time between blinks and shorter time for the blink, then go coroutine.

Also just realised a mistake I made and why your original code possibly did not work with the coroutine, when the gameObject active state was set as false, Unity won’t continue the coroutine after the yield. Instead of setting the active state as false, disable the MeshRender for the object maybe? I don’t know without knowing more.

IEnumerator Eyes()
{
    for (; ; )
    {
        // Set the rendering component .enabled value false here, for example,
        // GetComponent<MeshRenderer>().enabled = false;
        // But this is where it blinks,
        yield return new WaitForSeconds(0.2f); // Time to blink
        // Set the rendering component .enabled value true here, so it unblinks
        yield return new WaitForSeconds(3.0f); // Time between blinks
    }
}

So you’d replace the comments where it says to enable the renderer with whatever it is that makes it blink, just note that setting the gameObject as false for its active state, will cause the coroutine to not continue!

Obviously do not get the component every time, have a reference to it. Or replace it with however you are making it blink.

1 Like

Thank you very much sir problem solve :).I use this.

IEnumeratorEyes()
 {
for (; ; )
 {
//Settherenderingcomponent .enabledvaluefalsehere, forexample,
renderer.enabled=false;
//GetComponent<MeshRenderer>().enabled = false;
//GetComponent<MeshRenderer>().enabled=false;
yieldreturnnewWaitForSeconds(3.0f); //Timetoblink
//Settherenderingcomponent .enabledvaluetruehere
renderer.enabled=true;
yieldreturnnewWaitForSeconds(0.2f); //Timebetweenblinks
 }
 }
[code]