Spawning(?) problem

Updated on April 7, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on April 3, 2017

Hi, i have a flappy bird like game ,just added coins(coin,1 coin actually, that i made child of the column prefab so that it appears inbetween the columns).The problem is that after i collect 5 coins they don’t appear anymore ( 5 is my column pool size).Like, if i skip 1 or 2 they will appear again..My question is how can i make them continue spawning alongside the columns ?Thank you !

  • Liked by
Reply
0 on April 3, 2017

one way you could do this is to make a separate version of the prefab with the coin in and spawn that every now and then, so for example you could spawn 1 for every 3 normal pillars or for every 5 to match your pool, you could also add a list of the ones that have been collected and count the ones that aren’t collected and show that in the menu after the player looses to give them something to work for 

  • Liked by
Reply
Cancel
1 on April 4, 2017

Good idea man ! I’ll try it,thank you!
But do you know why don’t they spawn ? i mean the coin is in the prefab..
Anyway,Thank you again !

Helpful
on April 5, 2017

post your code for spawning and ill see what i can do

Show more replies
  • Liked by
Reply
Cancel
1 on April 5, 2017

public class ColumnPool : MonoBehaviour {
 public int columnPoolSize = 5;
 public GameObject columnPrefab;
 public float spawnRate = 4f;
 public float columnMin = 2.5f;
 public float columnMax = -2.5f;
 private GameObject[] columns;
 private Vector2 objectPoolPosition = new Vector2 (-15f, -25f);
 private float timeSinceLastSpawned;
 private float spawnXPosition = 10f;
 private int currentColumn = 0;
 // Use this for initialization
 void Start () 
 {
 bongs = new GameObject[columnPoolSize];
 for (int i = 0; i < columnPoolSize; i++) 
 {
columns [i] = (GameObject)Instantiate (columnsPrefab, objectPoolPosition, Quaternion.identity);
 }
 }
 // Update is called once per frame
 void Update () 
 {
 timeSinceLastSpawned += Time.deltaTime;
 if (GController.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;
 }
 }
 }
}

And the coin was later added as child of the columns prefab.
Thank you man !

Helpful
on April 7, 2017

it might be because you haven’t added the second prefab into the array add a new public gameobject called something like this :

public GameObject coincolumnPrefab;

at the top with the first one then have the spawning code pick at random using random.range between 0 and  columns -1 (unless you add more columns latter on) and spawn that one from the array

p.s sorry if that seems a bit confusing(i should really stop doing this at half 4 in the morning ) if you need more help do ask also if needed i’ll clarify that when iv’e had some sleep

Show more replies
  • Liked by
Reply
Cancel
0 on April 7, 2017

Managed it.Thank you very much, man !!

  • Liked by
Reply
Cancel