# I want there to only be 5 objects on screen at any time, can someone help please?

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);

}
}
``````

This logic is weird and almost certainly wrong:

``````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);
``````

Generate a Vector3 with x and z components between -6 and 6 and a y component of 0. If the x component is between -4 and 4 (and the y component is also between -4 and 4, which it always will be) then don’t increment the loop counter, otherwise create a Rock

Is that really what you wanted? As for why you’re getting 200, what is the value of `numRocks` set in the inspector?

That looks like it should only spawn 5 to me. Is the numRocks variable on the actual GameManager component in the scene still set to 5?