Unity (2018.2f and 5.6) Freezes when using While Loop for countdown timer in coroutine.

I am just writing a simple code to make a countdown timer in Unity 2018.2f, and upon writing the code and trying to run and test, Unity Engine freezes completely and I have to use Task Manager in my Windows OS to terminate everytime. But when I comment out While loop, Unity doesnt freeze.

I need a solution to this please. Either a reason on why and what I should change OR a workaround for a while loop, would be greatly appreciated !!

Thank you.


public class AI_AirStrike : MonoBehaviour {

public SphereCollider colliderhitchild;
private float dragTimer = 2f;

public void OnTriggerEnter(Collider other)
{

    if ( other.GetComponent<Collider>() == colliderhitchild )
    {
        Debug.Log("Hit something");
        StartCoroutine(RunDragTimer());
    }
}

public void OnTriggerExit(Collider other)
{
    if ( other.GetComponent<Collider>() == colliderhitchild )
    {
        Debug.Log("Exit something");
        StopCoroutine(RunDragTimer());
    }
}

private IEnumerator RunDragTimer()
    {
        yield return new WaitForSeconds(1f);
        while ( true )
        {

            dragTimer--;

            if ( dragTimer == 0f )
            {
                Debug.Log("time's up");
            }

        }
    }

}

It’s an infinite loop, it will freeze.

What you want to do is write “yield return” inside of the loop so each pass happen only once in a desired period of time.

I suppose that you want your RunDragTimer to wait 2 seconds and then log “time’s up”?

You should definitely not do this with an infinite while-loop inside the coroutine: The while-loop will repeat itself and not the coroutine which means that your timer will be reduced to 0 instantly and will keep repeating itself since you set the condition to true.


I am not quite sure whether you wanted your timer to stop on the OnTriggerExit or not, but here’s an alternative way to do it:

    public void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<Collider>() == colliderhitchild)
        {
            Debug.Log("Hit something");
            StartCoroutine(RunDragTimer());
        }
    }

    public void OnTriggerExit(Collider other)
    {
        if (other.GetComponent<Collider>() == colliderhitchild)
        {
            Debug.Log("Exit something");
            StopCoroutine(RunDragTimer());
        }
    }

    private IEnumerator RunDragTimer()
    {
        yield return new WaitForSeconds(1f);

        dragTimer--;

        if(dragTimer <= 0)
        {
            Debug.Log("time's up");
        }
        else
        {
            StartCoroutine(RunDragTimer());
        }
    }

Thanks for sharing this awesome post which is very informative. As i’m very thankful the article provider for this post. nice information on your blog.

TutuApp is the best & free third-party app store. Learn Here how to download https://tutuapp.zone/link text Vip helper for android, iOS & Windows PC for free.