> For the complete documentation index, see [llms.txt](https://gley.gitbook.io/mobile-traffic-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gley.gitbook.io/mobile-traffic-system/complete-api/addvehiclewithpath.md).

# AddVehicleWithPath

### Description

Instantiates a vehicle at the nearest waypoint to the provided position and sets the vehicle's path to the closest waypoint to the provided destination.

The instantiation occurs only when a vehicle of the specified type is available (disabled and ready for instantiation). The method will wait until a vehicle becomes available.

It is the developer's responsibility to choose a position out of view, as no additional verification will be made, and the vehicle might appear in front of the player.

### Declaration&#x20;

```csharp
public static void AddVehicleWithPath(Vector3 position, VehicleTypes vehicleType, Vector3 destination)

public static void AddVehicleWithPath(Vector3 position, VehicleTypes vehicleType, Vector3 destination, UnityAction<VehicleComponent, int> completeMethod)
```

### Parameters

<table><thead><tr><th width="188">Name</th><th>Description</th></tr></thead><tbody><tr><td><strong>position</strong></td><td>A Vector3 for the initial position. The vehicle will be placed on the closest waypoint from this position.</td></tr><tr><td><strong>vehicleType</strong></td><td>The type of the vehicle to be instantiated.</td></tr><tr><td><strong>destination</strong></td><td>A Vector3 for the destination position. The closest waypoint from this position will be the destination of the vehicle.</td></tr><tr><td><strong>completeMethod</strong></td><td>Callback triggered after instantiation. It returns the VehicleComponent and the waypoint index where the vehicle was instantiated. </td></tr></tbody></table>

### Example

```csharp
using Gley.TrafficSystem;
using UnityEngine;

public class Test : MonoBehaviour
{
    //a reference to vehicle pool and player assigned in inspector
    public VehiclePool vehiclePool;
    public Transform player;

    void Start()
    {
        API.Initialize(player, 20, vehiclePool);
    }

    //hand-picked positions
    Vector3 startPosition = new Vector3(0, 0, 0);
    Vector3 endPosition = new Vector3(0, 0, 350);

    public void AddVehicle()
    {
        API.AddVehicleWithPath(startPosition, VehicleTypes.Car, endPosition, CompleteMethod);
    }

    private void CompleteMethod(VehicleComponent vehicleScript, int waypointIndex)
    {
        Debug.Log($"{vehicleScript.name} was instantiated on waypoint {waypointIndex}");
    }
}

```
