I’ve set up a Color.Lerp in a coroutine to trigger off a collision between sprites, instead of fading from one color to another it instead flicks from the base color to the end color instantly despite a timer set up inside the coroutine.
Hey, this is actually a simple problem to solve =). As maccabe says, a coroutine will only yield execution when you tell it to.
The reason why the you end up going straight to the end colour within the same frame is because there’s no call to yield execution within the while loop.
There are a variety of things you can yield – WaitForSeconds(numberOfSeconds) is a common one (which does as you’d expect) and WaitForEndOfFrame will wait until the end of that frame. The thing is, neither of these are suitable in this case since you want to lerp every single frame.
Instead you can simply return null: yield return null. In this case nothing is returned and execution will simply pick up again on the next frame. This is a good way to iterate over a series of frames.
I’ve removed the WaitForEndOfFrame and added in yield return null in your code below. This should sort everything out =).