Skip to content

Instantly share code, notes, and snippets.

@Protonz
Created October 9, 2017 23:57
Show Gist options
  • Save Protonz/ec94a4c85e0f619063b83b676e74e593 to your computer and use it in GitHub Desktop.
Save Protonz/ec94a4c85e0f619063b83b676e74e593 to your computer and use it in GitHub Desktop.
Spawn prefabs at pre-defined spawn points. There can be a delay before spawning, then the prefabs can be spawned in batches with a delay between each batch.
using UnityEngine;
/// <summary>
/// Spawn prefabs at pre-defined spawn points.
/// There can be a delay before spawning, then the prefabs can be spawned in batches with a delay between each batch.
/// </summary>
public class ListSpawner : MonoBehaviour {
public GameObject[] PrefabList;
public Transform[] SpawnPoints;
public float SpawnDelay = 0f;
public float SpawnRepeatRate = 0.5f;
public int SpawnBatchSize = 3;
// The way of doing it with InvokeRepeating
void Start() {
InvokeRepeating("Spawn", SpawnDelay, SpawnRepeatRate);
}
int enemyId = 0;
void Spawn() {
for( int batchIndex = 0; batchIndex < SpawnBatchSize; batchIndex++ ) {
if( enemyId >= PrefabList.Length ) {
CancelInvoke("Spawn");
return;
}
Debug.Log("Spawning Enemy #" + enemyId);
var newGO = Instantiate(PrefabList[enemyId]);
int spawnPointIndex = enemyId % SpawnPoints.Length;
newGO.transform.position = SpawnPoints[spawnPointIndex].position;
enemyId++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment