- Home /
Create DoTween from for loop?
I'm trying to animate GUI values in my game that are stored as a list of integers. For this, I've tried to set up the following code using doTween.
for (int i = 0; i < importantShoppingStats.Length; i++)
{
if (oldShoppingStats[i].statNumber != importantShoppingStats[i].statNumber)
{
Debug.Log("NEW TWEEN!");
Tween valueTween = DOTween.To(() => (float)displayShoppingStats[i].statNumber, x => displayShoppingStats[i].statNumber = (int)x, (float)importantShoppingStats[i].statNumber, updateTime).SetOptions(false);
oldShoppingStats[i] = importantShoppingStats[i];
}
}
This seems to not animate the values at all. However, if I do this, it seems to work fine for the one value I'm setting.
if(oldShoppingStats[0] != importantShoppingStats[0])
{
Tween valueTween = DOTween.To(() => (float)displayShoppingStats[0].statNumber, x => displayShoppingStats[0].statNumber = (int)x, (float)importantShoppingStats[0].statNumber, updateTime).SetOptions(false);
}
I don't really understand what is happening differently when I put this if statement in the forloop that causes DoTween to act differently. I'll be honest, this is my first lambda expression and I don't have a very good grasp on that either. Any help would be greatly appreciated.
Answer by Bambivalent · Feb 21 at 12:28 PM
I hated this problem for years and forgot how I solved it in previous projects :D
I just found out that if you wrap each DOTween.To method in a coroutine, it works well!
This should work:
IEnumerator Do(int i) {
Tween valueTween = DOTween.To(() => (float)displayShoppingStats[i].statNumber, x => displayShoppingStats[i].statNumber = (int)x, (float)importantShoppingStats[i].statNumber, updateTime).SetOptions(false);
oldShoppingStats[i] = importantShoppingStats[i];
yield return null;
}
Starting the coroutine without quotes will allow you to pass the iterator:
StartCoroutine(Do(i));