> 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/addvehicle.md).

# AddVehicle

### Description

Adds a traffic vehicle to the nearest waypoint from the provided position. The standard Traffic System conditions, such as avoiding adding a vehicle if it is within the player's view, will be bypassed. It is the developer's responsibility to ensure that it is safe to add the vehicle.

This method will wait until a vehicle of that type is available, and the closest waypoint is free to add a new vehicle. The process runs in the background until the new vehicle is successfully added.

### Declaration&#x20;

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

public static void AddVehicle(Vector3 position, VehicleTypes vehicleType, 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>The position where to add a new vehicle. The closest waypoint to that position will be used.</td></tr><tr><td><strong>vehicleType</strong></td><td>The type of vehicle to add.</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);
    public void AddVehicle()
    {
        //0 - is an example index of the vehicle
        API.AddVehicle(startPosition, VehicleTypes.Car, CompleteMethod);
    }

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

```
