I can't solve it! [SOLVED]

I’m stuck at this lecture 99

the error said in the console NullReferenceException
The variable enemyPrefab of WaveConfig has not been assigned.
You should probably need to assign the enemyPrefab variable of the WaveConfig script in the inspector.

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

public class EnemySpawner : MonoBehaviour
{
[SerializeField] List waveConfigs;
int startingWave = 0;
// Start is called before the first frame update
void Start()
{
var CurrentWave = waveConfigs[startingWave];
StartCoroutine(SpawnAllEnemiesInWave(CurrentWave));
}

private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
    for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
    {
        Instantiate(waveConfig.GetEnemyPrefab(), waveConfig.GetWayPoints()[0].transform.position, Quaternion.identity);
        yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
    }
}

}

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

[CreateAssetMenu (menuName = “Enemy Waves Config”)]
public class WaveConfig : ScriptableObject
{
[SerializeField] GameObject EnemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweenSpawns = 0.1f;
[SerializeField] float spawnRandomFactor = 0.3f;
[SerializeField] int numberOfEnemies = 5;
[SerializeField] float moveSpeed = 2f;

public GameObject GetEnemyPrefab() 
{ 
    return EnemyPrefab; 
}

public List<Transform> GetWayPoints() 
{
    var WaveWaypoints = new List<Transform>();
    foreach (Transform child in pathPrefab.transform)
    {
        WaveWaypoints.Add(child);
    }


    return WaveWaypoints; 
}

public float GetTimeBetweenSpawns() { return timeBetweenSpawns; }

public float GetSpawnRandomFactor() { return spawnRandomFactor; }

public int GetNumberOfEnemies() { return numberOfEnemies; }

public float GetMoveSpeed() { return moveSpeed; }

}

please help me

thanks

I’m gonna reset back to lecture 95

I still need some replies here if the error is back

Hi,

Welcome to our community! :slight_smile:

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.

Is the problem solved?


See also:

It’s this line here:

[SerializeField] GameObject EnemyPrefab;

When you set SerializeField, you are giving yourself the option to go assign something to that variable through Unity rather than in code. But sometimes we forget to do that.

If you open the EnemyWave prefab in Unity, make sure it’s Enemy Prefab field has your enemy prefab in it. (Make sure it’s the prefab, not an enemy from your scene.)

Thank you dude!!!

thanks for helping me (All)

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

Privacy & Terms