Hey guys, I did look all over the forums for an answer to this question, but found nothing
The thing is, I have a function, that should increment a variable (X) in a while loop, something like this
while (x<5){ x++; }
The only problem is that the loop is carried out instantaneously (as it should), but I want the increment to be done once per frame rate (Time.deltaTime should be used I think), but I don’t really know how can I set a delay to each incrementing.
Any help would be appreciated!
Many thanks
You could use a timer variable and update that in the Update function like so:
public float delay = 1f;
private float timeout;
private float x;
void Update()
{
timeout -= Time.deltaTime;
if (timeout <= 0)
{
x++;
timeout += delay;
}
}
But it would probably be easier and more in line with your initial approach to use a coroutine:
public float delay = 1f;
void Start()
{
StartCoroutine(Foo(5));
}
IEnumerator Foo(int count)
{
int x = 0;
while (x < count)
{
x++;
yield return new WaitForSeconds(delay);
}
}
Both examples are C# btw, but should work equally well in UnityScript.