// Returns the specified object from the pool public InfiniteObject ObjectFromPool(int localIndex, ObjectType objectType) { InfiniteObject obj = null; int objectIndex = LocalIndexToObjectIndex(localIndex, objectType); List <InfiniteObject> objectPool = objectsPool[objectIndex]; int poolIndex = objectPoolIndex[objectIndex]; // keep a start index to prevent the constant pushing and popping from the list if (objectPool.Count > 0 && objectPool[poolIndex].IsActive() == false) { obj = objectPool[poolIndex]; objectPoolIndex[objectIndex] = (poolIndex + 1) % objectPool.Count; return(obj); } // No inactive objects, need to instantiate a new one InfiniteObject[] objects = null; switch (objectType) { case ObjectType.Platform: objects = platforms; break; case ObjectType.Scene: objects = scenes; break; case ObjectType.Obstacle: objects = obstacles; break; case ObjectType.Coin: objects = coins; break; case ObjectType.PowerUp: objects = powerUps; break; } obj = (GameObject.Instantiate(objects[localIndex].gameObject) as GameObject).GetComponent <InfiniteObject>(); AssignParent(obj, objectType); obj.SetLocalIndex(localIndex); objectPool.Insert(poolIndex, obj); objectPoolIndex[objectIndex] = (poolIndex + 1) % objectPool.Count; return(obj); }