My version of the Coroutine

For this lecture i challenged myself to came up with a code as soon i understood what Gary wanted to do, almost at the start so, totally missing his idea to refactor the foreach xD

And this is what i came up with:

IEnumerator FollowPath()
    {
        for (int i = 0; i < path.Count; i++)
        {
            transform.position = path.ElementAt(i).transform.position;
            yield return new WaitForSeconds(waitTime);
        }

for context here’s Gary’s version:

IEnumerator FollowPath()
    {
        foreach (Waypoint waypoint in path)
        {
            transform.position = waypoint.transform.position;
            yield return new WaitForSeconds(waitTime);
        }
    }

Gary’s version it’s surely way better, and i probably never thought by myself about using a foreach in that way, but atleast it works and i practiced some extra C# from my side study, and i call it a victory :muscle: :muscle:

Privacy & Terms