Help with Object Pool Spawning.
Hi Everybody,
I'm just taking my first steps into unity and c# after focusing on Python for the past year or so, mostly by following the Unity tutorials. Anyway, I followed the 'Flappy Bird Style' game, built it successfully and have been succeeding in adding extra elements to it and learning the code as I'm doing so.
However, I made the GameController increase the scroll speed each successful point scored (this works well, so haven't included any of the code), but this leads the spawning of columns from the object pool to spawn at increasingly large distances.
I then tried to adjust the spawn rate by decreasing it at the same rate that the score increases, however this ends up with the spawn rate being 0 and every column spawning at once.
Is there a way that I can set the columns to spawn after a set distance from the last spawned column?
If so could anyone let me know/at least point me in the right direction? Thank you in advance, and apologies, I am in my first week of C#.
ColumnPool.cs:
using System.Collections;
using UnityEngine;
public class ColumnPool : MonoBehaviour
{
public GameObject columnPrefab; //The column game object.
public int columnPoolSize = 15;
public float spawnRate = 3f;
public float columnMin = -1f;
public float columnMax = 3.5f;
private GameObject[] columns;
private int currentColumn = 0;
private Vector2 objectPoolPosition = new Vector2(-15, -25);
private float spawnXPosition = 10f;
private float timeSinceLastSpawned;
void Start()
{
timeSinceLastSpawned = 0f;
//Initialize the columns collection.
columns = new GameObject[columnPoolSize];
//Loop through the collection...
for (int i = 0; i < columnPoolSize; i++)
{
//...and create the individual columns.
columns[i] = (GameObject) Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
}
}
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
{
timeSinceLastSpawned = 0f;
//Set a random y position for the column
float spawnYPosition = Random.Range(columnMin, columnMax);
//...then set the current column to that position.
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
//Increase the value of currentColumn. If the new size is too big, set it back to zero
currentColumn++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
{
{
spawnRate = 4 - 0.3f * (GameController.instance.score); //<- this is where I am having trouble at the moment.
}
}
}
}
Formatted a bit for you, be sure to give a few lines between code and text. The formatting gets a bit confused sometimes. :-)
Answer by xxmariofer · Jan 31, 2019 at 07:51 PM
store a reference to the last spawnedwall, then rather checking the timeSinceLastSpawned , just change the if to
if(GameController.instance.gameOver == false && Vector3.Distance(new Vector3(spawnXPosition, spawnYPosition, 0), referenceToTheLastObject) >= desiredDistance) //desiredDistance is the distance to you want the objects to be spawning
Thank you so much! Haven't managed to get it to work yet, but I'm sure I can given a little while longer.
whats your exact issue? i have seen in your code you already have a reference to the last object since it is already stored in the pool so it should be easy to implement, whats the part giving you issues?
Well there are a few:
The name 'spawnYPosition' does not exist in the current context The name 'referenceToTheLastObject' does not exist in the current context The name 'desiredDistance' does not exist in the current context
However as far as I can see they do exist. I think I'm storing variables and objects incorrectly in the script.
Your answer
![](https://koobas.hobune.stream/wayback/20220612190837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object Pooling Help 0 Answers
Cannot convert void to UnityEngine.UI.Toggle.ToggleEvent - Javascript 0 Answers
I want Script To Move a object from a different object (In four Different directions,Randomly) . 0 Answers
How to move object a from in front of object b to in front of object c and transfer orientation? 1 Answer