What is Coroutine
There is a example of coroutine :
Image thisImage;
IEnumerator Fade()
{
int i=0;
Vector2 pos;
while(i<50)
{
pos.x = Mathf.Lerp(pos.x, 5, 3*Time.deltaTime );
yield return new WaitForSecond(.2f);
i++;
}
}
In the above Example we are changing value overtime, SO if we are moving objects horizontal position over time we can use coroutine.
2. How we will execute some code for function after particular interval of time. ?
3. How we can give some sec. delay in unity game. ?
4. What is IEnumerator in C#. ?
5. How to start and stop coroutine. ?
6. what is yield in coroutine ?
Image thisImage;
IEnumerator Fade()
{
int i=0;
Vector2 pos;
while(i<50)
{
pos.x = Mathf.Lerp(pos.x, 5, 3*Time.deltaTime );
yield return new WaitForSecond(.2f);
i++;
}
}
In the above Example we are changing value overtime, SO if we are moving objects horizontal position over time we can use coroutine.
Best uses of Coroutine in Unity games
1. Detecting some condition with fixed interval of time.
2. Performing animation over time.
3. To lerp value over time.
4. For instantiating bullet, Enemy and any effect with fixed period of time.
5. Updating UI elements over time.
6. Best alternative for Update function if we don't want to update things in each frame.
Questions on Coroutine
1. What is coroutine, why we use it. ?2. How we will execute some code for function after particular interval of time. ?
3. How we can give some sec. delay in unity game. ?
4. What is IEnumerator in C#. ?
5. How to start and stop coroutine. ?
6. what is yield in coroutine ?