void generateVehicle() { if (start != null && end != null) { GameObject vehicleObj = Instantiate(carModelPrefab, start.Value, Quaternion.identity); Vehicle vehicle = vehicleObj.GetComponent <Vehicle>(); if (vehicle.SetStart(start.Value) && vehicle.SetDest(end.Value, randomizeLane: true, initialSpeed: 10f)) { float leadingS = vehicle.VhCtrlOfCurrentSeg.GetIDMInfo(vehicle.LaneOn, 0).leadingS; if (leadingS < vehicle.bodyLength + new DriverBehavior().s0) { //Abort if there's no enough space to place vehicle.Abort(); Destroy(vehicleObj); } else { vehicle.stopEvent += delegate { Destroy(vehicleObj); vehiclesOfRoute.Remove(vehicleObj); }; vehiclesOfRoute.Add(vehicleObj); } return; } Destroy(vehicleObj); start = end = null; } }