A simpler LINQ statement

This code can be simplified a little for paths that alwasy go LEFT to RIGHT and only move UP.
By changing the type of path to IEnumerable<WayPoint> and then changing the entire method to

path = GameObject.FindGameObjectsWithTag("Path")
                .OrderBy(x => x.name)
                .Select(x => x.GetComponent<Waypoint>());

Note that we can take advantage of the previous work to name the objects by leveraging lexicographical sorting (OrderBy).
This is ineffcient though as we loop through all the objects twice and the sort is likely O(n squared)

1 Like

Thank you for sharing this

Privacy & Terms