I have an array from which spawns a random image. Instantiate spawning full pool on start

Hi, thanks for looking at my question.

My experience is minimal but I’m trying to learn through some trial and error.
I have an array which holds 4 prefabs from which 2 are instantiated at a time (and cycled)
My issue comes with columnPoolSize. If I set it to 1 it spawns 1 prefab as necessary but if I set it to 2 for example it will spawn both on start instead of just 1.

Is there a way I can change the poolSize effectively later ? I’ve tried various methods that visually seem to work by looking in the inspector however no change is noted when the game is running.

Thanks in advance !

public class ColumnPool : MonoBehaviour {

public int columnPoolSize;

private GameObject columnPrefab;
public GameObject[] prefabPool;

public float spawnRate = 22.5f;
public float columnMin = -1f;
public float columnMax = 3.5f;

private GameObject[] columns;
private Vector2 objectPoolPosition = new Vector2 (0f, 0f); //offscreen spawn
private float timeSinceLastSpawned;
private float spawnXPosition = 0f;
private int currentColumn = 0;

// Use this for initialization
void Start () {
    
    columns = new GameObject[columnPoolSize];
        for (int i = 0; i < columnPoolSize; i++)
    {
        int num = UnityEngine.Random.Range(0, prefabPool.Length);
        columnPrefab = prefabPool[num];

        columns *= (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);*

}
}

void Update () {
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0;
float spawnYPosition = Random.Range(columnMin, columnMax);
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
currentColumn++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}

  • }*
    }

You need a coroutine. The last line of the code waits for how many seconds you’d like before it executes
the the SpawnPrefab function again. And you need to start a routine by using the StartCoroutine(routineName()); function.

public float spawnRate;
    private void Start()
    {
        StartCoroutine(SpawnPrefabs());
    }
    
    IEnumerator SpawnPrefabs()
    {

        columns = new GameObject[columnPoolSize];
        for (int i = 0; i < columnPoolSize; i++)
        {
            int num = UnityEngine.Random.Range(0, prefabPool.Length);
            columnPrefab = prefabPool[num];
            columns *= (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);*

yield return new WaitForSeconds(spawnRate);
}
}