private IEnumerator UpdateConnection(PathElement newElement) { if (!newElement.Connected) { // Get Connected Elements with their direction and select only the ones that are connected to the start element; var connections = GetConnectedElements(newElement) .Where(element => element.bot.Connected) .Select(element => newElement.Connect(element.Bottom)).ToList(); if (connections.Count == 0) { yield break; } while (connections.Any()) { for (var i = connections.Count - 1; i >= 0; i--) { if (!connections[i].MoveNext()) { connections.RemoveAt(i); } } // Wait a frame yield return(null); } } newElement.Connected = true; GetConnectedElements(newElement).Where(e => !e.bot.Connected) .ForEach(e => StartCoroutine(UpdateConnection(e.bot))); }