private void HandleOutsideObjectRemoval(OutsideObject outsideObject) { // Only one of HandleOutsideRemoval should be called so unsubscribe it outsideObject.OnOutsideObjectRemovalDistance -= HandleOutsideObjectRemoval; // Remove the object from the collection. m_OutsideObjects.Remove(outsideObject); // Return the object to its object pool. m_OutsideObjectPool.ReturnGameObjectToPool(outsideObject.gameObject); }
private void SpawnOutsideObject() { // Get an outside object from the object pool. GameObject outsideGameObject = m_OutsideObjectPool.GetGameObjectFromPool(); // Generate a position at a distance forward from the camera within a random position between min and max distance and put the outside object at that position. int dir = Random.Range(0f, 1f) > 0.5 ? 1 : -1; var x = dir * Random.Range(m_OutsideObjectSpawnMinDistance, m_OutsideObjectSpawnMaxDistance); Vector3 outsideObjectPosition = m_Cam.position + Vector3.forward * m_SpawnZoneDistance + new Vector3(x, -m_OutsideObjectHeight, 0); outsideGameObject.transform.position = outsideObjectPosition; // Get the outside object component and add it to the collection. OutsideObject outsideObject = outsideGameObject.GetComponent <OutsideObject>(); m_OutsideObjects.Add(outsideObject); // Subscribe to the asteroids events. outsideObject.OnOutsideObjectRemovalDistance += HandleOutsideObjectRemoval; }