Download this documentation page as a PDF.

Contents

Advanced Usage

More advanced topics, including locating the source prefab from a pooled instance.

Getting the source prefab from a pooled instance

Sometimes you'll find that you need to know the source prefab, but all you have available is a pooled instance. If you're not using a manager, you'll need to know the pool to return to, and this will likely require knowing the source prefab. You might also have code that detects collisions between game objects, and tries to acquire an instance of the collided game object - but since they're pooled instances, you need to know the prefab first.

GameObjectPool

GameObject pools will automatically attach a PoolableGameObject component to all pooled instances, and this component exposes a public property SourceObject, which is the prefab (or regular object) that the pool is creating clones of.

Therefore, the following code can be used to get the prefab from a pooled instance:

var poolableComponent = pooledInstance.GetComponent<PoolableGameObject>();
var prefab = poolableComponent.SourceObject;

ComponentPool

For Component pools, the PoolableComponent component is automatically attached, which also exposes a SourceObject property. The value returned by the property is a SerialisableType, which provides a hot reload-safe representation of a regular Type. You can access the Type property from the SerialisableType to discover the underlying type.

The code would look something like this:

var poolableComponent = pooledInstance.GetComponent<PoolableComponent>();
var componentType = poolableComponent.SourceObject.Type;