NullReferenceException: Instatiate with construtor OR in Inspector

Hey Comm-unity,

i am currently working on a TowerDefense Game and want to implement an infinite level. I have a WaveSpawner with public HeaderVariables in which in can drag and drop Prefabs(Enemies) which will then be instatiated into the wave. These Header Variables are mostly arrays or Objects which include an Array and a number (To have different #Enemies for each run).
For the infite level i added constructors to the these header variables to initialize all waves virtually and not in the inspector. This worked but however when i switch back to the normal levels I donĀ“t get any Enemies. (I tracked the issue down to the constructor while using the Debugger but I could be wrong with that since i am very new to unity).

Maybe someone can smell what the issue is already. I am grateful for any help!

Thank you for your time. If can share my whole code if necessary.

Tim

NullReferenceException: Object reference not set to an instance of an object
Wave.Startactually () (at Assets/Script/Wave.cs:24)
WaveSpawner+c__Iterator0.MoveNext () (at Assets/Script/WaveSpawner.cs:116)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
WaveSpawner:Update() (at Assets/Script/WaveSpawner.cs:90)

Wave.cs

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class Wave{

    public EnemiesPerWave[] enemies;

    public bool shuffleBefore;
    public int killNodes;

    //private GameObject[] spawnList;
    private List<GameObject> spawnList = new List<GameObject>();
    private int enemyCounter = 0;

    

    public void Startactually()
    {
        foreach(EnemiesPerWave entry in enemies){
            for(int i = 0; i<=entry.count; i++)
            {
                spawnList.Add(entry.enemy);
                
            }
        }
    }


    public float rate;
    public GameObject getNextEnemy()
    {
        GameObject ret =  spawnList[enemyCounter];
        enemyCounter++;
        return ret;
    }
    public int count()
    {
        // number of total enemies
        return spawnList.Count;
    }
    public Wave(float rate, bool shuffleBefore, int killNodes, EnemiesPerWave[] wave1, bool isInfinity)
    {
        if (isInfinity)
        {
            Debug.Log("const");
            this.rate = rate;
            this.shuffleBefore = shuffleBefore;
            this.killNodes = killNodes;
            //this.enemies = null;
            this.enemies = wave1;

            Debug.Log(enemies.Length);
            this.Startactually();
        }
        else
        {
            this.Startactually();
        }


    } }

The issue here is that your enemies array is null. Unity would serialize the array in the inspector automatically, therefore making it not-null (but just have a length of 0).

You can either initialize the enemies array in code or use the Unity inspector