public void Clear()
        {
            while (spawned.Count > 0)
            {
                pool.Enqueue(spawned.Dequeue());
            }

            for (int i = 0; i < pool.Count; i++)
            {
                var instance = pool[i];
                DestroyImmediate(instance);
            }

            pool.Clear();
        }
        public GameObject Spawn(bool activate)
        {
            // Always return an object from the pool first if one is available
            if (pool.Count > 0)
            {
                var pooled = pool.Dequeue();
                spawnInstance(pooled, activate);
                return(pooled);
            }

            // If the pool is empty but the number of current prefab instances
            // has not been reached, instantiate a new instance
            if (maxInstances == -1 || spawned.Count < maxInstances)
            {
                var instantiated = Instantiate();
                spawnInstance(instantiated, activate);
                return(instantiated);
            }

            // Already hit the limit, and asked to return NULL
            if (limitType == LimitReachedAction.Nothing)
            {
                return(null);
            }

            // Already hit the limit and asked to throw an exception
            if (limitType == LimitReachedAction.Error)
            {
                throw new Exception(string.Format("The {0} object pool has already allocated its limit of {1} objects", PoolName, MaxInstances));
            }

            // Return the oldest already-spawned instance
            var recycled = spawned.Dequeue();

            spawnInstance(recycled, activate);
            return(recycled);
        }