# Spawner

This component allows to instantiate needed amount of copies of certain gameobject, in certain spawn point (or points, randomly) with certain time period.

{% hint style="info" %}
This could be useful for spawning AI enemies or NPCs in some game area.
{% endhint %}

### How to use

For faster experience, you can use Spawner prefab in `Assets/Heroic Engine/Prefabs/Gameplay` directory, drag onto your scene and just change some parameters in inspector if needed.

<figure><img src="https://1131436974-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9rEHd0nHFvWCSkMEfAjl%2Fuploads%2F1jjVK3dOchwwCqMhJTS8%2Fimage.png?alt=media&#x26;token=61e02ba7-0071-43bd-aa07-c8d0b8dedde6" alt=""><figcaption><p>Spawner prefab</p></figcaption></figure>

This sample spawner will spawn example object presented in the same folder. This object is just a cube with Rigidbody and [LifetimeObject](https://heroicsolo.gitbook.io/heroic-engine/basics/useful-components/lifetimeobject) component which returns this object back to pool after certain amount of time.

### Parameters in inspector

<figure><img src="https://1131436974-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9rEHd0nHFvWCSkMEfAjl%2Fuploads%2F3dNRGw8NNiAkTf0CDdsO%2Fimage.png?alt=media&#x26;token=46620578-9e62-4993-aa75-b9914fe7b48f" alt=""><figcaption><p>Spawner in inspector</p></figcaption></figure>

**Object Prefab** – prefab that will be spawned by this spawner. In case if it's PooledObject, it will be got from [PoolSystem](https://heroicsolo.gitbook.io/heroic-engine/basics/engine-utilities/poolsystem).

**Spawn Points** – points where these objects can be spawned (if more than 1 point assigned, they will spawn randomly on given points)

**Launch Mode** – in which way this spawner will be activated.

There are 3 options:

* None (spawner can be activated only via its Launch method)
* At Start (spawner will be activated automatically at start)
* On Trigger Enter (spawner can be activated if collider with appropriate layer enters this spawner trigger zone). Layer can be selected in appropriate field like shown below:

<figure><img src="https://1131436974-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9rEHd0nHFvWCSkMEfAjl%2Fuploads%2F72OfwD1CE4w5uRYb5UQs%2Fimage.png?alt=media&#x26;token=0535f92f-b8f5-4c10-a4d0-59f92a085aab" alt=""><figcaption><p>Trigger layer selection</p></figcaption></figure>

**Start Spawn Delay** – how much time spawner waits before starting spawn process.

**Spawn Period** – time period between spawn waves.

**Spawn Count** – how much objects should be spawned.

***

### Available methods

```csharp
public void Launch()
```

*This method launches spawning process.*

***

```csharp
public void Stop()
```

*This method stops spawning process.*

***

```csharp
public void RemoveSpawnedObjects()
```

*This method destroys all objects spawned by this spawner (if that objects were pooled, they return back to poll).*

***

```csharp
public void SetSpawnPeriod(float period)
```

*This method sets spawn period (in seconds).*

***

```csharp
public void SetSpawnObjectPrefab(GameObject prefab)
```

*This method sets spawn object prefab. If this prefab is inherited from PooledObject, it will be pooled via PoolSystem.*

***

```csharp
public void SetSpawnCallback(Action spawnCallback)
```

*This method sets a certain action which will be invoked each time when object will be spawned.*

***

```csharp
public void SetSpawnEndCallback(Action spawnEndCallback)
```

*This method sets a certain action which will be invoked in the end of whole spawning process.*
