- Home /
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");
}
}
}
}
Answer by Casiell · Aug 18, 2018 at 10:11 PM
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.
Answer by ignacevau · Aug 18, 2018 at 10:21 PM
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());
}
}
I really like this workaround. and yes, I wanted to stop the timer OnTriggerExit. Although, the other comment has got me covered on while loop. But I might just use this method of "if" in the future. Thank you
Answer by jackwilly1 · Aug 19, 2018 at 04:01 PM
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.
Answer by unity_19216811ipscom · Apr 09, 2019 at 08:15 AM
TutuApp is the best & free third-party app store. Learn Here how to download https://tutuapp.zone/[link text][1] Vip helper for android, iOS & Windows PC for free.
[1]: https://tutuapp.zone/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to create an application with "tracking objects without markers"? 0 Answers
** why does my Enum parameter goes back to default value? ** 1 Answer
How would I go about create multiple cursors controlled by multiple players 1 Answer