private IMyDoor GetNearestGateOnDifferentGrid(IMyDoor sourceGate) { List <Sandbox.ModAPI.IMySlimBlock> gateList = GetGateList(); double distance = 0.0d; IMyDoor nearest = null; //MyAPIGateway.Utilities.ShowNotification("Searching for portals", 250); foreach (var gate in gateList) { // Skip disabled, or destroyed gates // Skip if on same grid as source gate if (gate.IsDestroyed || !gate.FatBlock.IsFunctional || gate.FatBlock.GetTopMostParent().EntityId == sourceGate.GetTopMostParent().EntityId) { continue; } // THen find the closest. if (distance == 0.0d || (entrance_g.GetPosition() - gate.FatBlock.GetPosition()).Length() < distance) { nearest = gate.FatBlock as IMyDoor; distance = (entrance_g.GetPosition() - gate.FatBlock.GetPosition()).Length(); } } //if(nearest != null) //MyAPIGateway.Utilities.ShowNotification("Nearest Gate = " + nearest.CustomName, 250); return(nearest); }