Enemy spawn issue

The enemy not spawning in Laser defender currently at lecture 17.Got this error.

EnemySpawner file

console shows an error on line 13 which I wrote exactly the same from the lecture.

Hi Daniyal,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

ArgumentOutOfRange exception means that the code tried to access a non-existent index in an array, List or another collection. The first index in an array and List is 0, hence the last index is the length minus 1. For example, if the array has got a length of 2, and the code tries to access index 2, you will get this error message because there are only two “slots” in the array: at index 0 and at index 1.

Hope this helps :slight_smile:


See also;

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());
    }

}

}

Thank you so much Nina for your response. I will definitely put my code from now on whenever I have to ask any questions about code. I fully understand the solution you gave that I am going out of bounds in the list but I don’t understand how this code worked in the lecture?

Double click on the error message. To which line is it pointing?

And could you share a screenshot of the Enemy Spawner component in the Inspector?

1 Like

Thank you Nina to put my attention towards inspector.I missed attaching scriptable wave objects in enemySpawner object’s inspector. Sometimes I did very stupid mistakes. Thank you once again to give your precious time :slight_smile:

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

Privacy & Terms