some arrays ive initialized in awake are throwing null errors in my start function.

public class EnemySpawner1 : MonoBehaviour
{
    [SerializeField]
    private float _waitTime;
    [SerializeField]
    private GameObject _enemyContainer;
    [SerializeField]
    private GameObject[] _enemyToSpawn;
    [SerializeField]
    private Transform[] _spawnPoints = null;

    private int[] _enemyCount;
    private Vector3[] _spawnPoint;

    private bool[] _stopSpawn;


    private IEnumerator StartSpawn01, StartSpawn02;



    private void Awake()
    {
        int[] _enemyCount = new int[_enemyToSpawn.Length];
        bool[] _stopSpawn = new bool[2] {false, false };
        Vector3[] _spawnPoint = new Vector3[_spawnPoints.Length];
        StartSpawn01 = SpawnRoutine1(_waitTime);
        StartSpawn02 = SpawnRoutine2(_waitTime);
        Debug.Log(_stopSpawn[0]);
        Debug.Log(_enemyCount[0]);
        for (int i = 0; i < _spawnPoints.Length; i++)
        {

            _spawnPoint[i] = new Vector3(_spawnPoints[i].transform.position.x, _spawnPoints[i].transform.position.y, _spawnPoints[i].transform.position.z);
            Debug.Log("workign");
            Debug.Log(_spawnPoint[i]);
        }

    }

    // Start is called before the first frame update
    void Start()
    {



        Debug.Log(_stopSpawn[0]);
        Debug.Log(_enemyCount[0]);




        if (StartSpawn01 != null && _enemyToSpawn.Length >= 0)
        {
            Debug.Log(_stopSpawn[0]);
            Debug.Log(_enemyCount[0]);
            StartCoroutine(StartSpawn01);
        }
        if (StartSpawn02 != null && _enemyToSpawn.Length >= 2)
        {
            StartCoroutine(StartSpawn02);
        }










    }

    
    }

when i run the code the debug logs in awake show correct values, but first debug log in the start function throws a null reference exception error.

NullReferenceException: Object reference not set to an instance of an object
EnemySpawner1.Start () (at Assets/Pers/Scripts/EnemySpawner1.cs:50)

line 50 is the first debug log in start.

Lines 24 and 25 create fresh local variables. Those are NOT the class variables.

Remove the type declarators if you intend to access the class variables.

3 Likes

Am fool, much thanks.

1 Like