> For the complete documentation index, see [llms.txt](https://gley.gitbook.io/mobile-pedestrian-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-pedestrian-system/complete-api/addpedestrianwithdestination.md).

# AddPedestrianWithDestination

### Description

Adds a pedestrian to the nearest waypoint from the provided position and automatically sets a destination for it. The standard Pedestrian System conditions, such as avoiding adding a pedestrian 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 pedestrian.

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

### Declaration

```csharp
 public static void AddPedestrianWithDestination(Vector3 position, Vector3 destinationPosition, PedestrianTypes type, UnityAction<Pedestrian, int> completeMethod)
```

### Parameters

<table><thead><tr><th width="261">Name</th><th>Description</th></tr></thead><tbody><tr><td><strong>position</strong></td><td>The position where to add a new pedestrian.</td></tr><tr><td><strong>destinationPosition</strong></td><td>The destination point.</td></tr><tr><td><strong>type</strong></td><td>The type of the pedestrian to add.</td></tr><tr><td><strong>completeMethod</strong></td><td>Callback triggered after instantiation. It returns the PedestrianComponent and the waypoint index where the pedestrian was instantiated.</td></tr></tbody></table>

### Example

```csharp
using Gley.PedestrianSystem;
using UnityEngine;
public class Test : MonoBehaviour
{
    // A reference to the pedestrian pool and player assigned in the inspector.
    public PedestrianPool PedestrianPool;
    public Transform Player;

    void Start()
    {
        API.Initialize(Player, 1, PedestrianPool);
    }

    public void AddPedestrian()
    {
        // Find the object called Destination and go to it.
        Vector3 destination = GameObject.Find("Destination").transform.position;

        // Add a pedestrian close to the player and make it go to the destination.
        API.AddPedestrianWithDestination(Player.position, destination, PedestrianTypes.Normal, CompleteMethod);
    }

    private void CompleteMethod(Pedestrian pedestrianCOmponent, int waypointIndex)
    {
        Debug.Log($"Pedestrian {pedestrianCOmponent.name} was added to waypoint {waypointIndex}");
    }

    public void RemovePedestrian()
    {
        // Remove the pedestrian with index 0.
        API.RemovePedestrian(0);
    }

    // Used for demonstration purposes.
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            AddPedestrian();
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            RemovePedestrian();
        }
    }
}
```
