- Home /
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);
}
}
Answer by tanoshimi · Jun 18, 2014 at 02:10 PM
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?
I interpreted that as trying to prevent them from spawning in the middle of the screen. Odd way to write it (I'd have a counter of rocks created, and "while (rocksCreated < numRocks)" loop personally, but I don't think it's wrong.
Yes my apologies, I changed the numRocks to 5 for testing. It should actually be 200. I agree, the logic is a bit weird, but what it does is it stops the rocks from respawning at the player's position.
@Rob-Fireproof - but pos.y
is initialised at 0, and then immediately tested to see if pos.y > -4 && pos.y < 4
.... ;)
@tanoshimi - Oh yeah! I see what you mean. I'm guessing those should be z's... :-)
"I interpreted that as trying to prevent them from spawning in the middle of the screen. Odd way to write it (I'd have a counter of rocks created, and "while (rocksCreated < numRocks)" loop personally, but I don't think it's wrong."
@Rob-Fireproof Do you think you could show me how? Though my code works somewhat correctly, it seems to glitch every now and then, where the rocks will sometimes go out of range.
Answer by Rob-Fireproof · Jun 18, 2014 at 02:06 PM
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?
Yes my apologies, I changed the numRocks to 5 for testing. It should actually be 200.
Hang on, so you want 200 and you're only getting 5, even after assigning the numRocks variable in the inspector to 200?
@tanoshimi No, I have set the numRocks value to 200. Now although there are 200 rocks, I want only 5 of the 200 rocks to appear on camera at any time. Do you think something like that is possible?
Your answer
![](https://koobas.hobune.stream/wayback/20220613151829im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple Cars not working 1 Answer
WaitForSeconds/Yield problem 1 Answer