Wave one repeats. Cannot catch with debugger - Lecture 101

As the title says in lecuter 101 of the Complete C# Unity Game Developer 2D course.

I am having an issue whereby for an odd reason, my first wave is repeating itself. I have debugged the process multiple times, although I cannot seem to catch it in the debugger.

Half way through the 4th wave - the first wave happens again without being processed in my debugger (ie. it doesn’t ever hit that part) So I am assuming it has something to do with the yielding of my coroutines.

Has anyone else had this issue?

Below is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class EnemySpawner : MonoBehaviour
{
    // config params
    [SerializeField] List<WaveConfig> waveConfigs;
    [SerializeField] int startingWave = 0;
 
    // state Variables
 
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(SpawnAllWaves());
    }
 
    private IEnumerator SpawnAllEnemiesinWave(WaveConfig currentWave)
    {
        for (int i = 0; i < currentWave.GetNumberOfEnemies(); i++)
        {
            var newEnemy = Instantiate(
                currentWave.GetEnemyPrefab(),
                currentWave.GetWaypoints()[0].transform.position,
                Quaternion.identity);
            newEnemy.GetComponent<EnemyPathing>().setWaveConfig(currentWave);
            yield return new WaitForSeconds(currentWave.GetTimeBetweenSpawns());
        }
    }
 
    private IEnumerator SpawnAllWaves()
    {
        for (int i = startingWave; i < waveConfigs.Count; i++)
        {
            var currentWave = waveConfigs[i];
            yield return StartCoroutine(SpawnAllEnemiesinWave(currentWave));
        }
    }
}

Hi,

Welcome to our community! :slight_smile:

May I ask how you debugged your project? Did you use Debug.Logs anywhere?

And how many EnemySpawner objects are there in your scene? One EnemySpawner object is supposed to spawn the waves assigned to waveConfigs in the Inspector of a game object in your Hierarchy.

Hi there,

I debug using my IDE (currently using visual studio code). I only have one enemy spawner object present.

I have placed a Debug.Log within my spawn “specific” wave coroutine. It only fires 4 times like it is supposed to. Yet I get 5 sometimes 6 waves instead of 4. And the speed values of those waves seem to be the baseline of my waveConfig files.

I might restart this project. but I find it rather odd that I have “rogue” waves that dont even show logs or get hit by the debugger

I restarted the program from scratch. Seems to be working now. I think it might be something to do with an issue in the duplication of my waveconfig files. Perhaps some backend issue in Unity itself as the code is pretty much the same.

Should I delete this post or just mark this reply as the solution?

If restarting the project fixed the issue for you, that’s a valid solution. If I cannot figure the problem out within a reasonable amount of time, I often restart because that’s usually faster than looking for the issue.

Sometimes, Unity is fairly weird. Especially when we duplicate Unity objects such as scenes or, as in your case, scriptable objects, it might be that there are suddenly problems with references. The more complex a Unity file is, the more bothersome it’ll become to find the problem in that file. If you are curious, open a scriptable object (not your script!) or a scene file with a script editor. That’s how many things look behind the scenes.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms