public void SplitUnitGroups(BaseTile invokingRoom, List <BaseTile> roomList) { var oldId = (roomList[0].RoomScript as BaseUnitRoom).GroupId; Dictionary <Guid, int> IdAndRoomSize = new Dictionary <Guid, int>(); foreach (var room in roomList) { var newId = CreateUnitGroup(); var count = Astar.ConvertRoom(room, newId, invokingRoom); IdAndRoomSize.Add(newId, count); } var antContainer = GameObject.Find("Ants"); List <Ant> ants = new List <Ant>(); foreach (Transform child in antContainer.transform) { if (child.GetComponent <Ant>() != null) { if (child.GetComponent <Ant>().unitGroupID == oldId) { ants.Add(child.GetComponent <Ant>()); } } else { throw new Exception("The ant doesnt have the required script!"); } } IdAndRoomSize.Add(oldId, 0); int totalCount = 0; foreach (var pair in IdAndRoomSize) { var uGroup = MindGroupList.GetUnitGroupFromUnitId(pair.Key); int count = 0; for (int i = totalCount; (pair.Value > 0 && i < totalCount + pair.Value) && i < ants.Count; i++) { ants[i].unitGroupID = pair.Key; count++; } if (pair.Value == 0) { uGroup.SetMaxUnits(uGroup.MaxUnits - totalCount); } else { uGroup.SetMaxUnits(pair.Value); } uGroup.SetCurrentUnits(count, true); for (int i = totalCount; i < totalCount + pair.Value && i < ants.Count; i++) { ants[i].unitGroupID = pair.Key; } totalCount += pair.Value; } }