I have a rather simple class. Its job is to spawn an enemy that I pass in every 1-5 seconds. For some reason the WaitForSeconds()
call isn’t working. When I’ve removed the call and used just the Instantiate()
call it spawns a single Enemy object just fine. I’ve looked at other examples and I can’t seem to get it to work. I’m not sure what I’m doing wrong. Occasionally the test 1 log gets printed. The test 2 log never gets printed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
bool spawn = true;
[SerializeField] float minSpawnDelay = 1f;
[SerializeField] float maxSpawnDelay = 5f;
[SerializeField] Enemy enemyPrefab;
// Start is called before the first frame update
IEnumerable Start()
{
print("test 1");
while (spawn) {
yield return new WaitForSeconds(Random.Range(minSpawnDelay, maxSpawnDelay));
SpawnEnemy();
}
}
private void SpawnEnemy() {
print("test 2");
Instantiate(enemyPrefab, transform.position, transform.rotation);
}
// Update is called once per frame
void Update()
{
}
}
The Game Object