# Destroy or Pool Object

The `DestroyOrPoolObject` class will automatically put objects back into the [**Object Pool**](https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/object-pooling). This is very useful for things that are created by [**Projectiles**](https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/projectile), but aren't themselves part of the Projectile.

The demo scenes often make use of this with *"Impact"* and *"Muzzle"* particles. These `Prefabs` are brought into the scene by the [**Projectile Collision Behaviors**](https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/behaviors), but are separate objects. The **Projectile Power Destroy** class allows them to put themselves back into the pool when they're done.

<figure><img src="https://2431624982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MY3N_li2jPq7az6mYfq%2Fuploads%2FRnukec9zDcVFmz8GvhYE%2FScreenshot%202024-04-02%20at%207.02.48%E2%80%AFPM.png?alt=media&#x26;token=da045166-7389-4b43-b113-670c8e06a81a" alt=""><figcaption></figcaption></figure>

### Time To Destroy

This is the amount of time before the object is destroyed after the `RemoveGameObject()` method is called.

### Trigger When Projectile Set

When true, the `RemoveGameObject()` method will be called as soon as the `Projectile` is set. Generally this is immediate, meaning the object will be destroyed in `TimeToDestroy` time.

### Objects to Disable Immediately

If you want to disable specific objects (children of this object) immediately, prior to the object being destroyed, you can add them to this `List<>`.

### Delay To Disable Objects

The objects in the `List<>` will be disabled with this delay -- often you want this to be non-zero, so that those objects have a chance to "start" before they are disabled.

Reenable Them On Enable

When true, the objects in the `List<>` will be enabled whenever the object with this component on it is enabled. Otherwise, they will remain disabled. Generally this should be `true`.

## Using the Component

The `SetProjectile()` method triggers the entire operation.

The [**Projectile Utilities**](https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/additional-scripts/projectile-utilities) class has a `ProjectilePowerDestroySetProjectile()` extension method for `GameObject` which takes in a `Projectile`, and will call the `SetProjectile()` method with that value.

This method will return false if the `GameObject` does not have a `ProjectilePowerDestroy` component.

### Behaviors

Any [**Behavior**](https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/behaviors) which spawns an object should call this method on the spawned object, to ensure that the object is used with the [**Object Pool**](https://infinitypbr.gitbook.io/magic-pig-games/projectile-factory/projectile-factory-documentation/object-pooling) appropriately. In the example below, the `SpawnObjectOnDestroy` **Behavior** will spawn an object using this pattern.

```csharp
// SpawnObjectOnDestroy.cs

// Spawn the projectile. (This could be used with a normal GameObject too, with 
// slight changes to the code!)
protected virtual void SpawnTheObject(Projectile projectile)
{
     if (objectToSpawn == null) return;
            
     if (projectile.useObjectPool && ProjectilePoolManager.instance != null)
          RetrieveObjectFromPool(projectile);
     else
          CreateNewObject(projectile);
}
        
// Get it from the pool -- if null, instantiate it
private void RetrieveObjectFromPool(Projectile projectile)
{
     var pooledObject = ProjectilePoolManager.instance.GetProjectile(objectToSpawn);
     if (pooledObject != null)
          PrepareObject(pooledObject, projectile);
     else
          CreateNewObject(projectile);
}

// Instantiate the object
private void CreateNewObject(Projectile projectile) 
     => PrepareObject(Instantiate(objectToSpawn, projectile.transform.position + offset, Quaternion.identity), projectile);

// Set up the object -- here we call the ProjectilePowerSetProjectile() method,
// to ensure the object is sent back to the pool appropriately.
private void PrepareObject(GameObject projectileObject, Projectile projectile)
{
     projectileObject.SetActive(false);
     if (lookAtSpawner) 
          projectileObject.transform.LookAt(projectile.transform.position);
    
     projectileObject.transform.position = projectile.transform.position + offset;
     projectileObject.ProjectilePowerDestroySetProjectile(projectile);
     projectileObject.SetActive(true);
}
```

## Variations

### With Fade

The `DestroyOrPoolObjectWithFade` class adds additional options to fade common particle components including the particle materials (alpha), audio, lights, trail renderers, and line renderers. Each can be toggled on and off, and you can also choose to include children of this object or not.

The values will be reset to their start values when the object is re-enabled.

## Creating Your Own Version

The script requires the use of a `Projectile` because the *"Destroy"* or *"Pool"* logic is determined by the logic assigned to the `Projectile`.

This class has virtual members, meaning you can create an override that behaves differently, allowing you to make use of the logic with your own system, perhaps one which does not require a `Projectile`, or requires another object unique to your project.
