I’m actually so confused-- Unity is calling a nullref on my Laser Defender StartCoroutine(). I’ve scoured the internet for a solution, but the GameObject the script is on hasn’t been destroyed, so I really don’t know what’s going on. Here’s my code, could someone please help me find the error? I’d really appreciate it. Thanks!
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
WaveConfigSO currentWave;
List waveSOs;
[SerializeField]bool isLooping = true;
[SerializeField] Waveset_SO currentWaveset;
void Start()
{
StartCoroutine(WaveEnemies());
waveSOs = currentWaveset.GetCurrentWaveset();
}
public WaveConfigSO GetCurrentWave()
{
return currentWave;
}
IEnumerator WaveEnemies()
{
do
{
foreach (WaveConfigSO wave in waveSOs)
{
currentWave = wave;
for (int j = 0; j < currentWave.GetEnemyCount(); j++)
{
Instantiate(currentWave.GetEnemyPrefab(j), currentWave.GetFirstPoint().position, Quaternion.identity, transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}
yield return new WaitForSeconds(currentWaveset.GetTimeBetweenWaves());
}
}
while (isLooping);
}
}
NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
WaveConfigSO currentWave;
List<WaveConfigSO> waveSOs;
//[SerializeField] float timeBetweenWaves;
[SerializeField]bool isLooping = true;
[SerializeField] Waveset_SO currentWaveset;
void Start()
{
StartCoroutine(WaveEnemies());
waveSOs = currentWaveset.GetCurrentWaveset();
}
public WaveConfigSO GetCurrentWave()
{
return currentWave;
}
IEnumerator WaveEnemies()
{
do
{
foreach (WaveConfigSO wave in waveSOs)
{
currentWave = wave;
for (int j = 0; j < currentWave.GetEnemyCount(); j++)
{
Instantiate(currentWave.GetEnemyPrefab(j), currentWave.GetFirstPoint().position, Quaternion.identity, transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}
yield return new WaitForSeconds(currentWaveset.GetTimeBetweenWaves());
}
}
while (isLooping);
}
This is the code. The error is on line 14, where it says StartCoroutine(WaveEnemies()); The nullref message is “object reference not set to instance of an object,” yet I can’t find any blank fields anywhere in my inspector or prefabs.
I SOLVED IT, NEVERMIND! Line 14 and 15 had to be switched, because I added a waveset system where I’d have predetermined groups of waves. The Coroutine was being called, but because it starts by instantiating enemies for each wave IN THE WAVESO, the scriptable object didn’t exist yet because it hadn’t been ‘gotten’ by the GetCurrentWaveset() line. Sorry for the trouble, everyone. I appreciate your patience and help.