- Home /
Spawning - How to only have one object at a spawn at a time?
Hi! I'm currently working on a 2D game where there are a total of 8 spawn points that spawn orbs (the spawn points are chosen at random). I'm new to scripting, and so I'm having trouble preventing the orbs from spawning at the same spawn as another orb. If anyone could help me with my script that would be great!!
Here's the code I've made:
var spawnTargets : GameObject[];
var newTarget : Transform;
function Start () {
spawnTargets = GameObject.FindGameObjectsWithTag("Spawn");
}
InvokeRepeating("PlaceOrb", 1.0, 1.0);
function PlaceOrb () {
var spawnPos = spawnTargets[(Random.Range(0, spawnTargets.Length))].transform.position; //randomly return position of a spawn point
Instanciate(newTarget, spawnPos, Quaternion.identity);
}
It does a good job of randomly selecting a spawn point, but I'd like it if there was a maximum of 8 orbs total and only one per spawn point at any given time.
Here's a picture to help explain it better:
Thank you! :)
picturetoexplain.jpg
(336.9 kB)
Comment