Ask coding questions

← Back to all posts
Unity help again
Bunnytoes (139)

I have my enemy and the scripts attached it will spawn 1 enemy and won't spawn the 2nd wave, please help here is code

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class WaveSpawner : MonoBehaviour
{

    public Transform enemyPrefab;

    public Transform spawnPoint;

    public float timeBetweenWaves = 6.5f;
    private float countdown = 3f;

    public Text WavecountdownText;

    private int waveIndex = 0;

    void Update ()
    {
        if (countdown <= 0f)
        {
           StartCoroutine(SpawnWave());
            countdown = timeBetweenWaves;
        }

        countdown -= Time.deltaTime;

        WavecountdownText.text = Mathf.Round(countdown).ToString();
    }

    IEnumerator SpawnWave ()
    {
        for (int i = 0; i < waveIndex; i++)
        {
            waveIndex++;

            SpawnEnemy();
            yield return new WaitForSeconds(0.5f); 
        }

    }

    void SpawnEnemy ()
    {
        Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
    }

}

here are the screenshots

what should I do? should I turn the enemy into a prefab?
and also I attached the enemy prefab to the enemy prefab in gamemaster, but what should I do in enemy, should I attach wavespawner to enemy?

Answered by SixBeeps (5221) [earned 5 cycles]
View Answer
Comments
hotnewtop
SixBeeps (5221)

I dunno why it wouldn't spawn past the first enemy, but this looks sus

Since you're increasing the upper bound each time you loop, it'd go on infinitely. The condition in a for-loop gets re-evaluated on each iteration.

Bunnytoes (139)

sorry for the ping @SixBeeps but I can't figure out what's wrong, I think I can't figure out what to attach, but please help

again sorry for ping

Bunnytoes (139)

please someone help