- Home /
How to solve while loop lags
Hello everybody I use while loop in 2 Coroutines. 1 coroutine increase number during time and second move 2 texts during same time. I call this 2 coroutines in same function. Sometimes my android game freezes for second. Do you have any ideas how to solve this problem? Here is my code:
IEnumerator IncreaseNumber()
{
float duration = 0.5f;
float elapsed = 0;
while (elapsed<duration)
{
elapsed += Time.deltaTime;
graduatingNumber2 = Mathf.Lerp(0, randomValueSpeed2, elapsed / duration);
yield return null;
}
}
IEnumerator MoveSpeedTextHasTextSmoothly()
{
float currentTime = 0;
float timeOfTravel = 0.5f;
while (currentTime <= timeOfTravel)
{
currentTime += Time.deltaTime;
secondSpeedText.rectTransform.anchoredPosition = Vector2.Lerp(startPositionSpeedTextVector2, endPositionSpeedTextVector2, currentTime/timeOfTravel);
secondHasText.rectTransform.anchoredPosition = Vector2.Lerp(startPositionHasTextVector2, endPositionHasTextVector2, currentTime / timeOfTravel);
yield return null;
}
}
Comment
Answer by Bodhid · Sep 01, 2017 at 03:27 PM
Well the first while loop does not contain a yield return null
Maybe that is the cause of your freeze?