So I’ve been stuck here for a few hours now. If I load my “game” scene first, everything runs fine.
If I load my “main menu” scene first then click the start button to load the “game” scene, the “game” scene loads but enemy waves don’t spawn and I get this null exception error:
NullReferenceException: Object reference not set to an instance of an object
WaveConfig.GetWaypoints () (at Assets/Scripts/WaveConfig.cs:26)
EnemySpawner+<SpawnAllEnemiesInWave>d__6.MoveNext () (at Assets/Scripts/EnemySpawner.cs:36)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <a1ac446df41c4a67becf2f8317dc1792>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
<SpawnAllWaves>d__5:MoveNext() (at Assets/Scripts/EnemySpawner.cs:28)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
(WaveConfig.cs:26 is the foreach loop that calls my pathprefab variable under the “getwaypoints” method)
The enemyspawner object still has all the waveconfig files referenced at runtime and the waveconfig scriptable objects still have the paths referenced. I’ve checked all references at runtime and they all seem to be fine, so I can’t figure out why this null exception happens.
The biggest issue is that this seems to be random. I implemented 3 waves and SOMETIMES wave 3 does actually run instead of giving me a null reference. Some other times, I’d have all 3 waves run, then when I go to the quit scene, then start menu scene, then game scene again, only wave 3 would start or no wave would start. This “randomness” seems to change everytime Unity recompiles after I change anything in the scripts on visual studio. I can’t reproduce the few times it decides to work. This makes me think that the issue could be in the script execution order but I tried playing around there and still no luck.
None of the solutions that other users here or on stackoverflow that I could find fixed the issue for me.
The only consistency here is that it runs fine if I start by loading my “game” scene and that I get a null reference when I load the “game” scene from the another scene.