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 *= (GameObject)Instantiate(bronzePrefab, coinPoolPos, Quaternion.identity);*
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 *= (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)*