Search Unity

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

Discussion in 'Editor & General Support' started by neils711, Apr 27, 2021.

  1. neils711

    neils711

    Joined:
    Mar 27, 2020
    Posts:
    3
    Code (CSharp):
    1. public class EnemySpawner1 : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private float _waitTime;
    5.     [SerializeField]
    6.     private GameObject _enemyContainer;
    7.     [SerializeField]
    8.     private GameObject[] _enemyToSpawn;
    9.     [SerializeField]
    10.     private Transform[] _spawnPoints = null;
    11.  
    12.     private int[] _enemyCount;
    13.     private Vector3[] _spawnPoint;
    14.  
    15.     private bool[] _stopSpawn;
    16.  
    17.  
    18.     private IEnumerator StartSpawn01, StartSpawn02;
    19.  
    20.  
    21.  
    22.     private void Awake()
    23.     {
    24.         int[] _enemyCount = new int[_enemyToSpawn.Length];
    25.         bool[] _stopSpawn = new bool[2] {false, false };
    26.         Vector3[] _spawnPoint = new Vector3[_spawnPoints.Length];
    27.         StartSpawn01 = SpawnRoutine1(_waitTime);
    28.         StartSpawn02 = SpawnRoutine2(_waitTime);
    29.         Debug.Log(_stopSpawn[0]);
    30.         Debug.Log(_enemyCount[0]);
    31.         for (int i = 0; i < _spawnPoints.Length; i++)
    32.         {
    33.  
    34.             _spawnPoint[i] = new Vector3(_spawnPoints[i].transform.position.x, _spawnPoints[i].transform.position.y, _spawnPoints[i].transform.position.z);
    35.             Debug.Log("workign");
    36.             Debug.Log(_spawnPoint[i]);
    37.         }
    38.  
    39.     }
    40.  
    41.     // Start is called before the first frame update
    42.     void Start()
    43.     {
    44.  
    45.  
    46.  
    47.         Debug.Log(_stopSpawn[0]);
    48.         Debug.Log(_enemyCount[0]);
    49.  
    50.  
    51.  
    52.  
    53.         if (StartSpawn01 != null && _enemyToSpawn.Length >= 0)
    54.         {
    55.             Debug.Log(_stopSpawn[0]);
    56.             Debug.Log(_enemyCount[0]);
    57.             StartCoroutine(StartSpawn01);
    58.         }
    59.         if (StartSpawn02 != null && _enemyToSpawn.Length >= 2)
    60.         {
    61.             StartCoroutine(StartSpawn02);
    62.         }
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73.     }
    74.  
    75.    
    76.     }

    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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.
     
    mopthrow, Joe-Censored and neils711 like this.
  3. neils711

    neils711

    Joined:
    Mar 27, 2020
    Posts:
    3
    Am fool, much thanks.
     
    Kurt-Dekker likes this.