Created
October 9, 2017 23:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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