Greetings,
I’m optimizing one of my games and have added object pooling. I’m using the typical approach where I have a dictionary mapping prefab names to a queue.
It’s working fabulously except for one prefab. It’s a pretty simple GameObject with some particle effects (explosion).
When I spawn a new object, if I didn’t find an existing one in the pool that’s not already active I Instantiate a new one and immediately call DontDestroyOnLoad on it and then add it to the pool.
It’s working great inside the scene and it gets used repetitively, no problem.
But when I Load a new scene it’s getting destroyed (confirmed with a breakpoint in OnDestroy() on an attached script.
I also noted that that gameobject doesn’t show up under DontDestroyOnLoad in the scene. I step through in the debugger and see the DontDestroyOnLoad() getting executed but it doesn’t get added to DontDestroyOnLoad in the scene.
Here’s where I instantiate new gameobjects and add them to the pool
private GameObject InstantiateGameObject(GameObject prefab, bool setActive)
{
var queue = GetQueue(prefab);
var gameObject = Instantiate(prefab);
DontDestroyOnLoad(gameObject);
gameObject.SetActive(setActive);
queue.Enqueue(gameObject);
return gameObject;
}
Here’s the script that’s attached to the gameobject that will deactivate it when the particle effect stops
// If true, deactivate the object instead of destroying it
public bool OnlyDeactivate;
void OnEnable()
{
StartCoroutine("CheckIfAlive");
}
private void OnDestroy()
{
Debug.LogError($"{name} is being destroyed.");
}
IEnumerator CheckIfAlive ()
{
ParticleSystem ps = this.GetComponent<ParticleSystem>();
while(true && ps != null)
{
yield return new WaitForSeconds(0.5f);
if(!ps.IsAlive(true))
{
if (OnlyDeactivate)
{
#if UNITY_3_5
this.gameObject.SetActiveRecursively(false);
#else
this.gameObject.SetActive(false);
#endif
}
else
{
GameObject.Destroy(this.gameObject);
}
break;
}
}
}
And yes, I’ve confirmed that OnlyDeactivate is true. The object is being destroyed when loading a new scene, not when the particle effect stops.
Here’s a screenshot showing where the object has been Instantiated, DontDestroyOnLoad is called and it’s added to the pool:
Note that it doesn’t appear under DontDestroyOnLoad afterwards

