Destroy or Pool Object

v1.0

The DestroyOrPoolObject class will automatically put objects back into the Object Pool. This is very useful for things that are created by Projectiles, 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, but are separate objects. The Projectile Power Destroy class allows them to put themselves back into the pool when they're done.

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 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 which spawns an object should call this method on the spawned object, to ensure that the object is used with the Object Pool appropriately. In the example below, the SpawnObjectOnDestroy Behavior will spawn an object using this pattern.

// 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.

Last updated