Today I'm trying to use coroutine to do the animation for Renderer's alpha value (fade effect), it's a lot simpler than modifying the alpha value in Update method.
1 2 3 4 5 6 7 8 |
IEnumerator Fade() { for (float f = 1f; f >= 0; f -= 0.1f) { Color c = renderer.material.color; c.a = f; renderer.material.color = c; yield return; } } |
Using the code from Unity Coroutine documentation will cause following error message:
Expression expected after yield return
Modifying yield return to yield return null will solve this problem. So the correct code should be like:
1 2 3 4 5 6 7 8 |
IEnumerator Fade() { for (float f = 1f; f >= 0; f -= 0.1f) { Color c = renderer.material.color; c.a = f; renderer.material.color = c; yield return null; } } |