public void SpawnObstacle(Vector3 position, Vector2i lane) { if (!m_Death) { int random = Random.Range(0, m_ObjectPool.Count); if (m_ObjectPool.Count > 0) { ObstaclePair fromPool = m_ObjectPool[random]; if (fromPool.data.CanSpawn(lane)) { m_ObjectPool.RemoveAt(random); fromPool.obstacle.transform.position = new Vector3(position.x, position.y, 20); fromPool.obstacle.gameObject.SetActive(true); fromPool.SetLifetime(4); m_ObjectsInUse.Add(fromPool); } } else { Debug.LogError("Pool Empty..."); } } }
public void InitializeObjectPool() { m_ObjectsInUse = new List <ObstaclePair>(); m_ObjectPool = new List <ObstaclePair>(); for (int i = 0; i < m_Obstacles.Length; i++) { for (int j = 0; j < m_Obstacles[i].objectPoolAmount; j++) { GameObject obstacle = Instantiate(m_Obstacles[i].obstacleObject, m_ObjectPoolPosition); ObstaclePair obstaclePair = new ObstaclePair(obstacle, m_Obstacles[i]); m_ObjectPool.Add(obstaclePair); } } }
void ToObjectPool(ObstaclePair pair) { pair.obstacle.gameObject.transform.position = m_ObjectPoolPosition.position; pair.obstacle.gameObject.SetActive(false); m_ObjectPool.Add(pair); }