A Few Questions

There are few things i didn’t understand well. Would be great if you guys explain. Would be even better if you can explain with examples and analogies. This is how i learn best :slight_smile:

  1. What’s going on in the line I’ve added a comment to?
    private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
        for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
            var newEnemy = Instantiate(waveConfig.GetEnemyPrefab(), waveConfig.GetWaypoints()[0].transform.position, Quaternion.identity);
            newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);    //What's going on here?
            yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
  2. I really didn’t get the foreach loop part. Can you explain it step by step?
    public List<Transform> GetWaypoints()
        var waveWaypoints = new List<Transform>();
        foreach (Transform child in pathPrefab.transform)
        return waveWaypoints;


  1. Using the instantiated object, newEnemy (an instance of your prefab), you are calling the GetComponent method to return a specific component of that GameObject. The type you are trying to get is EnemyPathing, this is the EnemyPathing.cs script component you added GameObject/prefab.

    The code makes a rather large assumption and assumes it is going to actually get that component, and continues to call the SetWaveConfig method of the EnemyPathing script component, it passes in the waveConfig as a parameter for that method to then use.

    If the component could not be found, a NullReferenceException error would occur because you try to access a member (the SetWaveConfig method) of it when it’s reference is null.

  2. The foreach statement is used to iterate through something.

    In this specific case you have the pathPrefab object which has a transform property. If a GameObject is a parent, then the child GameObjects transform components can be accessed in this way, as a collection. So your statement is saying, “for each child transform in this transform, do something”. Specifically, add the child transform to the waveWaypoints list.

Hope this helps :slight_smile:

Note, it’s always easier for people to offer to help if your post is clear and easy to read. With code, please always apply code formatting and make it as tidy/easy to read as possible, indenting really helps. :slight_smile:

See also;

@Hedaelus, does the above resolve your query?

See also;

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

Privacy & Terms