I am making an asteroids like game and I made 200 rocks respawn, however, I want there to only be 5 of the 200 rocks respawned on screen at any time. Can someone help please?
Here is my code.
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public GameObject[] RockPrefab;
public int numRocks = 200;
public GameObject rock;
public Vector3 pos;
public GameObject RocketPrefab;
// Use this for initialization
void Start ()
{
for (int i = 0; i < numRocks; i++) {
pos = new Vector3 (Random.Range (-6F, 6F), 0, Random.Range (-6F, 6F));
if (pos.x < 4 && pos.x > -4 && pos.y > -4 && pos.y < 4) {
i--;
} else
Rock (pos, 1.0f);
}
}
// Update is called once per frame
void Update ()
{
}
public void Rock (Vector3 RockPos, float rockSize)
{
rock = Instantiate (RockPrefab [Random.Range (0, 4)], RockPos, Quaternion.identity) as GameObject;
rock.transform.localScale = new Vector3 (rockSize, rockSize, 0f);
}
}