- Home /
Question by
airbagnr1 · Nov 27, 2015 at 10:22 AM ·
spawningif statementenemies
Max Spawning Obejcts
I'm making a FPS in c# (iv just started scripting) and I have a script that spawns objects at 3 different locations every 5 seconds. I have implanted some code that prevents more than 5 objects to spawn but for some reason this is not working. This is my code:
public class RandomBadGuySpawn : MonoBehaviour {
public GameObject spawnPrefab;
public float spawnTime = 5;
public Transform[] spawnPoints;
int maxBadGuys = 5;
public int currentBadGuys = 0;
// Use this for initialization
void Start () {
InvokeRepeating("Spawn", spawnTime, spawnTime); //The time between each Milk Spawned so 5sec then spawn and wait another 5
}
// Update is called once per frame
void Update () {
}
public void Spawn()
{
if (currentBadGuys >= maxBadGuys)
return;
Debug.Log("Max BadGuys");
int spwanPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(spawnPrefab, spawnPoints[spwanPointIndex].position, spawnPoints[spwanPointIndex].rotation);
}
}
I'm also wondering if it is possible to stop objects spawning after you destroy the objects and after a certain amount of seconds they spawn back? Thanks for the help if any, much appreciated!
Comment