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[i] = (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;
}
}
}
}
Answer by JusSumGuy · May 24, 2018 at 07:37 PM
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[i] = (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
yield return new WaitForSeconds(spawnRate);
}
}
Thanks so much, This has improved the result I've been getting in that it always initially spawns 1. However something is still causing some odd pooling in the beginning. 1 is spawned and snaps back ins$$anonymous$$d of a 2nd being spawned and after that it behaves normally spawning a 2nd and looping.
I suspect it's in the update function somewhere.
Thanks again, I will play around with what you've shown me
So I resolved the issue completely by putting an if statement on my Update functions so that the first clone would continue running and then a loop would start after. I wouldn't have been able to get it right without your help. Thanks again.
setValue = setValue + 3; (placed in private void start)
void Update() {
if (setValue >= 4) { //if statement allows first clone to pass through
timeSinceLastSpawned += Time.deltaTime;
Debug.Log("this is give us a new");
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0;
float spawnYPosition = Random.Range(column$$anonymous$$in, column$$anonymous$$ax);
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
currentColumn++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
}
}
Good Job! Glad to help I was actually stuck on a similar problem a few days ago lol. All it takes is to keep trying :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612163330im_/https://answers.unity.com/themes/thub/images/avi.jpg)