Code Reference‎ > ‎SpawnPool‎ > ‎

.Spawn()

Overload Summary

Transform Spawn(Transform prefab)    
Transform Spawn(Transform prefab, Vector3 pos, Quaternion rot

Transform Spawn(Transform prefab, Transform parent)    
Transform Spawn(Transform prefab, Vector3 pos, Quaternion rotTransform parent) 

-- Note these overloads which take gameObjects simply call the ones above after getting gameObject.transform --
Transform Spawn(GameObject prefab)    
Transform Spawn(GameObject prefab, Vector3 pos, Quaternion rot

Transform Spawn(GameObject prefab, Transform parent)    
Transform Spawn(GameObject prefab, Vector3 pos, Quaternion rotTransform parent) 


Transform Spawn(string prefab)    
Transform Spawn(string prefab, Vector3 pos, Quaternion rot

Transform Spawn(string prefabTransform parent)  
Transform Spawn(string prefab, Vector3 pos, Quaternion rotTransform parent)

ParticleSystem Spawn(ParticleSystem prefab, Vector3 pos, Quaternion rot)
ParticleSystem Spawn(ParticleSystem prefab, Vector3 pos, Quaternion rot
                     Transform parent)

ParticleEmitter Spawn(ParticleEmitter prefab, Vector3 pos, Quaternion rot)
ParticleEmitter Spawn(ParticleEmitter prefab, Vector3 pos, Quaternion rot, 
                      string colorPropertyNameColor color)

Added in PoolManager 5.5.0...
AudioSource Spawn(AudioSource prefab)
AudioSource Spawn(AudioSource prefab, Vector3 pos, Quaternion rot)
AudioSource Spawn(AudioSource prefabTransform parent)
AudioSource Spawn(AudioSource prefab, Vector3 pos, Quaternion rot, Transform parent)


Documentation

Spawn(Transform prefab)    

returns Transform or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Use Spawn() instead of Unity's Instantiate(). The function signature is the same but the return type is different and Spawn() will use an instance from the pool if one is available.

You will rarely need this version (overload) of Spawn(). Generally you will want to set the position and rotation on spawn (see the documentation below). This version is provided for completeness and for internal use by PoolManager when preloading as the position and rotation don't mater until spawned later.

PoolManager.Pools["Enemies"].Spawn(enemyPrefab);



Spawn(Transform prefab, Vector3 pos, Quaternion rot

returns
 
Transform 
or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Sets the rotation and the translation on spawn.

PoolManager.Pools["Enemies"].Spawn
(
    enemyPrefab,
    Vector3.zero,
    Quaternion.identity
);



Spawn(Transform prefab, Transform parent)    

returns Transform or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Adds an optional parent to be set. If you pass null, the instance will be un-parented so it is directly in the scene root.

PoolManager.Pools["Enemies"].Spawn(enemyPrefab, parent);



Spawn(Transform prefab, Vector3 pos, Quaternion rotTransform parent) 

returns
 
Transform 
or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Adds an optional parent to be set. If you pass null, the instance will be un-parented so it is directly in the scene root.

PoolManager
.Pools[
"Enemies"].Spawn
(
    enemyPrefab,
    Vector3.zero,
    Quaternion.identity,
    parent
);



Spawn(string prefab)    

returns Transform or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Allows the name of a prefab to be used instead of a prefab reference. This requires the prefab is in the SpawnPool whereas the reference will also add the prefab the first time it is used.

This is a convenience overload. You can do the same thing by getting a reference to the prefab using the SpawnPool.prefabs dictionary and then calling the regular overload of Spawn.

PoolManager.Pools["Enemies"].Spawn("EnemyA");




Spawn(string prefab, Vector3 pos, Quaternion rot

returns
 
Transform 
or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Allows the name of a prefab to be used instead of a prefab reference. This requires the prefab is in the SpawnPool whereas the reference will also add the prefab the first time it is used.

This is a convenience overload. You can do the same thing by getting a reference to the prefab using the SpawnPool.prefabs dictionary and then calling the regular overload of Spawn.

PoolManager
.Pools[
"Enemies"].Spawn
(
    "EnemyA",
    Vector3.zero,
    Quaternion.identity
);



Spawn(string prefabTransform parent)  

returns Transform or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
This connivence overload combines the ability to use the string name of the prefab and provide a parent. Read the documentation above for detail.

PoolManager.Pools["Enemies"].Spawn("EnemyA", parent);



Spawn(string prefab, Vector3 pos, Quaternion rotTransform parent)

returns
 
Transform 
or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
This connivence overload combines the ability to use the string name of the prefab and provide a parent. Read the documentation above for detail.

PoolManager.Pools["Enemies"].Spawn
(
    "EnemyA",
    Vector3.zero,
    Quaternion.identity,
    parent
);



Spawn(ParticleSystem prefab, Vector3 pos, Quaternion rot)

returns ParticleSystem or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
This will spawn a prefab which contains a Shuriken ParticleSystem, and when all particles die, despawn the instance automatically. This is similar to Unity's old ParticleEmitter auto-destruct feature except the instance will be pooled to be spawned again. Do NOT use Despawn() on instances which use this function.  There is no auto-destruct in Unity for the new particle system, so this is a nice value-added feature.

Note that you must pass a ParticleEmitter to use this version of Spawn() and that this will turn off emit before despawn, so use this again next time or spawning will not emit particles.

PoolManager.Pools["Enemies"].Spawn(prefabWithEmitter,
                                   Vector3.zero,
                                   Quaternion.identity);



Spawn(ParticleSystem prefab, Vector3 pos, Quaternion rot, Trasform parent)

returns ParticleSystem or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Adds an optional parent to be set. If you pass null, the instance will be un-parented so it is directly in the scene root.

PoolManager.Pools["Enemies"].Spawn(prefabWithEmitter,
                                   Vector3.zero,
                                   Quaternion.identity);



Spawn(ParticleEmitter prefab, Vector3 pos, Quaternion rot)

returns ParticleEmitter or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
This will spawn a prefab which contains a particle emitter then trigger emit, listen for all particles to die and then despawn the instance automatically. This is similar to Unity's auto-destruct feature except the instance will be pooled to be spawned again. Do NOT use Despawn() on instances which use this function.  (If a Particle Animator's auto-destruct is turned on, this function will turn it off so the instance isn't accidentally destroyed which would cause null reference errors.)

Note that you must pass a ParticleEmitter to use this version of Spawn() and that this will turn off emit before despawn, so use this again next time or spawning will not emit particles.

PoolManager.Pools["Enemies"].Spawn(prefabWithEmitter,
                                   Vector3.zero,
                                   Quaternion.identity);



Spawn(ParticleEmitter prefab, Vector3 pos, Quaternion rot, 
                string colorPropertyNameColor color)

returns
 
ParticleEmitter or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
This will spawn a prefab which contains a particle emitter then trigger emit, listen for all particles to die and then despawn the instance automatically. This is similar to Unity's auto-destruct feature except the instance will be pooled to be spawned again. Do NOT use Despawn() on instances which use this function.  (If a Particle Animator's auto-destruct is turned on, this function will turn it off so the instance isn't accidentally destroyed which would cause null reference errors.)

Note that you must pass a ParticleEmitter to use this version of Spawn() and that this will turn off emit before despawn, so use this again next time or spawning will not emit particles.

This function also sets a color property to a specified color. This is handy if you want to use the same explosion instance in two situations where the color is the only difference. It is important to note that this sets the material of the instance, not the sharedInstance, which can cause extra draw calls. In most cases this is not a big deal, but it should be considered if spawning many many instances at once.

// Spawn an explosion on an enemy.
Vector3 explosionPos = enemy.transform.position;

PoolManager.Pools["Enemies"].Spawn(enemyPrefab,
                                   Vector3.zero,
                                   Quaternion.identity,
                                   "_TintColor",
                                   Color.red);



Spawn(AudioSource prefab)    

returns AudioSource or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
To use this overload, pass a reference to an AudioSource component to automatically play and the despawn the instance.

PoolManager.Pools["Enemies"].Spawn(audoPrefab);



Spawn(AudioSource prefab, Vector3 pos, Quaternion rot

returns
 
AudioSource or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Sets the rotation and the translation on spawn. This is handy for 3D sounds.

PoolManager.Pools["Enemies"].Spawn
(
    audoPrefab,
    Vector3.zero,
    Quaternion.identity
);



Spawn(AudioSource prefab, Transform parent)    

returns AudioSource or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Adds an optional parent to be set. If you pass null, the instance will be un-parented so it is directly in the scene root.

PoolManager.Pools["Enemies"].Spawn(audoPrefab, parent);



Spawn(AudioSource prefab, Vector3 pos, Quaternion rotTransform parent) 

returns
 
AudioSource or nullnull is only returned if the 'limit' option was used and the limit was reached. Otherwise, there is no reason to test for null.

Description
Adds an optional parent to be set. If you pass null, the instance will be un-parented so it is directly in the scene root.

PoolManager
.Pools[
"Enemies"].Spawn
(
    audoPrefab,
    Vector3.zero,
    Quaternion.identity,
    parent
);