Hi, I picked up unity less than week ago so I don’t realy know what’s wrong with this code. I’ve read that you can’t use WaitForSeconds();
in Update
but I don’t know why and I don’t know how to do it otherwise. Can somebody help me how should I do it?
public class Spawning : MonoBehaviour
{
public GameObject zombiePrefab;
int randomSpawnZone;
float randomPositionX, randomPositionY;
Vector3 spawnPosition;
public float enemySpawned = 5f;
public float enemysToKill = 5;
public float enemyAddedEveryRound = 1;
void Start ()
{
for (int i = 0; i < enemySpawned; i++)
{
SpawnNewEnemy();
}
}
void Update ()
{
EnemyWaveSpawner();
}
IEnumerator Wait()
{
yield return new WaitForSeconds(3);
}
void SpawnNewEnemy()
{
randomSpawnZone = Random.Range(0, 4);
switch (randomSpawnZone)
{
case 0:
randomPositionX = Random.Range(-10f, 10f);
randomPositionY = Random.Range(-6f, -5f);
break;
case 1:
randomPositionX = Random.Range(-10f, 10f);
randomPositionY = Random.Range(5f, 6f);
break;
case 2:
randomPositionX = Random.Range(10f, 11f);
randomPositionY = Random.Range(-5f, 5f);
break;
default:
randomPositionX = Random.Range(-11f, -10f);
randomPositionY = Random.Range(-5f, 5f);
break;
}
spawnPosition = new Vector3(randomPositionX, randomPositionY, 0f);
Instantiate(zombiePrefab, spawnPosition, Quaternion.identity);
}
void EnemyWaveSpawner()
{
if (enemysToKill == 0)
{
Wait();
enemySpawned += enemyAddedEveryRound;
enemysToKill = enemySpawned;
for (int i = 0; i < enemySpawned; i++)
{
SpawnNewEnemy();
}
}
}
}