[C#] Creating falling coins
So I've managed to work up this CoinPooler script for my game. Issue I'm having is none of the coins are spawning on the x axis, they are all clumped behind on another making it impossible for a player to collect the coins. Any suggestions on how to fix this? My code will be listed below.
Yes, I'm using a form of ObjectPooling and Instantiate & Destroy. I tried using normal object pooling but I kept having problems with it when I was destroying the previous game objects.
private GameObject[] pooledBronze;
public GameObject silverCoin;
public GameObject goldCoin;
public GameController gameControl;
public GameObject bronzePrefab;
private int bronzeCoinAmount = 40;
private int silverCoinAmount = 2;
private int goldCoinAmount = 1;
private int currentBronzeCoin = 0;
private int currentSilverCoin = 0;
private int currentGoldCoin = 0;
public float coinObjMin = -3f;
public float coinObjMax = 9f;
private float spawnYPos = 10;
public float timeToSpawn = 0f;
private Vector2 coinPoolPos = new Vector2(-3, 10);
// Use this for initialization
void Start () {
gameControl.GetComponent<GameController>();
}
// Update is called once per frame
void FixedUpdate () {
timeToSpawn += 2.0f * Time.deltaTime;
if (timeToSpawn >= 9f)
{
spawnCoins();
timeToSpawn = 0f;
}
}
private void spawnCoins()
{
pooledBronze = new GameObject[bronzeCoinAmount];
for (int i = 0; i < bronzeCoinAmount; i++)
{
pooledBronze[i] = (GameObject)Instantiate(bronzePrefab, coinPoolPos, Quaternion.identity);
}
if (gameControl.gameOver == false && gameControl.gameStart && timeToSpawn >= 60.0f)
{
float spawnXPos = Random.Range(coinObjMin, coinObjMax);
pooledBronze[currentBronzeCoin].transform.position = new Vector2(spawnXPos, spawnYPos);
currentBronzeCoin++;
if(currentBronzeCoin >= bronzeCoinAmount)
{
currentBronzeCoin = 0;
}
if(currentBronzeCoin == 0)
{
spawnCoins();
}
}
}
Answer by kalen_08 · Jul 22, 2018 at 01:54 AM
There's a lot in there to go over... i think its been done quite poorly so maybe delete it and re-write this in a better way...
private GameObject[] pooledBronze;
public GameObject silverCoin;
public GameObject goldCoin;
public GameController gameControl;
public GameObject bronzePrefab;
private int bronzeCoinAmount = 40;
private int silverCoinAmount = 2;
private int goldCoinAmount = 1;
private int currentBronzeCoin = 0;
private int currentSilverCoin = 0;
private int currentGoldCoin = 0;
public float coinObjMin = -3f;
public float coinObjMax = 9f;
private float spawnYPos = 10;
public float timeToSpawn = 0f;
private Vector2 coinPoolPos = new Vector2(-3, 10);
// Use this for initialization
void Start () {
// this does nothing. it gets the component but
// does nothing with it nor assign it to something
gameControl.GetComponent<GameController>();
}
// Update is called once per frame
void FixedUpdate () {
timeToSpawn += 2.0f * Time.deltaTime;
if (timeToSpawn >= 9f)
{
spawnCoins();
timeToSpawn = 0f;
}
}
// overall this method is quite senseless and should be re-done
private void spawnCoins()
{
var thing = new GameObject[0];
pooledBronze = new GameObject[bronzeCoinAmount];
for (int i = 0; i < bronzeCoinAmount; i++)
{
// coinPoolPos never changes which is why they all spawn in the same spot.
pooledBronze[i] = (GameObject)Instantiate(bronzePrefab, coinPoolPos, Quaternion.identity);
}
// this block below will never get called.
// in void FixedUpdate () if timeToSpawn >= 9 then you spawn and then set it to 0
// therefor it will never be > 60.
if (gameControl.gameOver == false && gameControl.gameStart && timeToSpawn >= 60.0f)
{
float spawnXPos = Random.Range(coinObjMin, coinObjMax);
pooledBronze[currentBronzeCoin].transform.position = new Vector2(spawnXPos, spawnYPos);
currentBronzeCoin++;
if(currentBronzeCoin >= bronzeCoinAmount)
{
currentBronzeCoin = 0;
}
// if the statement above is called then this will be called so
// factor this out.
if(currentBronzeCoin == 0)
{
// this could end up being a recurring method == not good.
spawnCoins();
}
// this is a combination of the above 2 if statements into one simple one.
if(currentBronzeCoin >= bronzeCoinAmount || currentBronzeCoin == 0)
{
currentBronzeCoin = 0;
spawnCoins();
}
}
}
Okay... Well the critique is nice, however. Despite this fact, that wasn't really helpful in any way whatsoever. You say it was done "poorly" yet you offer no other advice other than "Re-do it". That doesn't really answer the question I was asking nor does it actually do anything for me. Not being a "negative nancy" or trying to be disrespectful in anyway but for future reference if you don't know what is going on then you shouldn't reply in the first place. I come here for scripting help, not the common sense gibberish I could get on Reddit. Just saying...
Your answer
![](https://koobas.hobune.stream/wayback/20220612171203im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I get an error message every time i run this script? Any ideas? 0 Answers
Unable to set position of instantiated prefab 1 Answer
Particle Systems. Stick inside pooled objects or pool them separately. 1 Answer
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers