Example #1
0
 void Update()
 {
     if (Time.time > spawnTime + lifeTime)
     {
         MyCache.Destroy(gameObject);// get rid of the entity if too old
     }
 }
Example #2
0
        public GameObject GetNextObjectInCache()
        {
            GameObject obj = null;

            // The cacheIndex starts out at the position of the object created
            // the longest time ago, so that one is usually free,
            // but in case not, loop through the cache until we find a free one.
            for (int i = 0; i < cacheSize; i++)
            {
                obj = objects[cacheIndex];

                // If we found an inactive object in the cache, use that.
                if (!obj.active)
                {
                    break;
                }

                // If not, increment index and make it loop around
                // if it exceeds the size of the cache
                cacheIndex = (cacheIndex + 1) % cacheSize;
            }

            // The object should be inactive. If it's not, log a warning and use
            // the object created the longest ago even though it's still active.
            if (obj.active)
            {
                Debug.LogWarning(
                    "Spawn of " + prefab.name +
                    " exceeds cache size of " + cacheSize +
                    "! Reusing already active object.", obj);
                MyCache.Destroy(obj);
            }

            // Increment index and make it loop around
            // if it exceeds the size of the cache
            cacheIndex = (cacheIndex + 1) % cacheSize;

            return(obj);
        }