Search Unity

Question Spawning Random Prefab from Object Pool

Discussion in 'Getting Started' started by Gummi_Ghost, Mar 19, 2022.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I wrote an object pooling script that's supposed to set active a random prefab from an object pool active every time the camera moves some random distance to the right. So far, all it does is Instantiate and set the prefabs inactive on awake. I can't get it to reference or set active anything from the list, and I'm not sure what else to try. I've tried multiple things from multiple sources, including the Unity - Scripting API: ObjectPool<T0> (unity3d.com), but most object pooling examples online are something like "pool exactly 20 identical bullets" and it's hard to find something like "spawn random prefab from pool". I would appreciate any feedback/help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Pool;
    5.  
    6. public class EOMovement : MonoBehaviour
    7. {
    8.     //camera distance vars
    9.     private int spawnDistanceGoal;
    10.     private int randomStepNumber;
    11.  
    12.     //object pooling vars
    13.     private int randomListObject;
    14.     public static ObjectPool<GameObject> SharedInstance;
    15.     public List<GameObject> allObjectPool;
    16.     public int randomObjectPoolNumber;
    17.     public GameObject currentObjectInPool;
    18.  
    19.     void Awake()
    20.     {    
    21.         for(int i = 0; i < allObjectPool.Count; i++)
    22.         {
    23.         currentObjectInPool = Instantiate(allObjectPool[i]);
    24.         currentObjectInPool.SetActive(false);
    25.         }
    26.     }
    27.  
    28.     void Start()
    29.     {      
    30.         randomStepNumber = Random.Range(1, 7);
    31.         spawnDistanceGoal = (DistanceTravledDisplay.distanceTravled + randomStepNumber);
    32.         StartCoroutine(Spawn());
    33.     }
    34.  
    35.     private IEnumerator Spawn()
    36.     {      
    37.         if (DistanceTravledDisplay.distanceTravled >= spawnDistanceGoal)
    38.         {
    39.             Debug.Log("Something should spawn now!");
    40.             randomObjectPoolNumber = Random.Range(0, allObjectPool.Count);
    41.             currentObjectInPool = (allObjectPool[randomObjectPoolNumber]);
    42.             currentObjectInPool.SetActive(true);
    43.             //Pooling and Instantiation code would probably go here
    44.             yield return new WaitForSeconds(2);
    45.             randomStepNumber = Random.Range(1, 7);
    46.             spawnDistanceGoal =  (DistanceTravledDisplay.distanceTravled + randomStepNumber);
    47.             StartCoroutine(Spawn());        
    48.         }
    49.         else
    50.         {
    51.             yield return new WaitForSeconds(2);
    52.             StartCoroutine(Spawn());
    53.         }
    54.     }
    55.  
    56. }
    57.