Problem at 10 minute mark of lecture

EDIT: Nevermind! Solved it, I needed to re-drag the Wave configs onto the Enemy Spawner object in Inspector, somehow they had disappeared.

I will leave the initial question below just in case someone else has the same issue.

When I run my program at the 10 minute mark of the lecture, no enemies instantiate and the console gives me this error.

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at :0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at :0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at :0)
EnemySpawner.Start () (at Assets/Scripts/EnemySpawner.cs:14)

Here is my EnemySpawner.cs

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

public class EnemySpawner : MonoBehaviour
{

    [SerializeField] List<WaveConfig> waveConfigs;
    int startingWave = 0; //(Sets startingWave to Wave1 Waveconfig i.e. element 0 in Player Spawner gameobject)

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("hello");
        var currentWave = waveConfigs[startingWave]; //new variable created to track the current wave, initially set to starting wave as defined above
        StartCoroutine(SpawnAllEnemiesInWave(currentWave)); //triggers spawn coroutine with current wave
    }

    private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
    {
       
        Instantiate(
            waveConfig.GetEnemyPrefab(), //GetEnemyPrefab [waveConfig.cs method to instantiate enemy]
            waveConfig.GetWaypoints()[0].transform.position, //GetWaypoints [waveConfig.cs method to get waypoints], [0] refers to the startingindex number which will be incremented
            Quaternion.identity);
       

        yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns()); //runs the GetTimeBetweenSpawns method which returns the time for WaitForSeconds to use.
    }
}

And here is my WaveConfig.cs

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


[CreateAssetMenu(menuName = "Enemy Wave Config")] //note this added line as well - seems to allow right clicking on the asset window to create new individual  Wave Config files
public class WaveConfig : ScriptableObject //note that Monobehaviour has been changed to ScriptableObject (not sure why)
{
    // the following are a bunch of variables that will be accessed by other scripts.
    [SerializeField] GameObject enemyPrefab;
    [SerializeField] GameObject pathPrefab;
    [SerializeField] float timeBetweenSpawns = 0.5f;
    [SerializeField] float spawnRandomFactor = 0.3f;
    [SerializeField] int numberOfEnemies = 5;
    [SerializeField] float moveSpeed = 2f;

    // public methods allowing other scripts to access the above variables
    public GameObject GetEnemyPrefab()   { return enemyPrefab; }


    //this method specifically creates a list of the transforms of each waypoint and returns it.
    public List<Transform> GetWaypoints() //public list as it needs to be accessible from other scripts
    {
        var waveWaypoints = new List<Transform>(); //creates temporary variable which will inherit list
        foreach (Transform child in pathPrefab.transform) //finds each child in the pathPrefab.transform (individual waypoints)
        {
            waveWaypoints.Add(child); //adds each waypoint the list
        }

        return waveWaypoints; // returns the list to the script that calls it.
    }

    public float GetTimeBetweenSpawns() { return timeBetweenSpawns; }
    public float GetSpawnRandomFactor() { return spawnRandomFactor; }
    public int GetNumberOfEnemies() { return numberOfEnemies; }
    public float GetMoveSpeed() { return moveSpeed; }
}

1 Like

I had the same exact error but I had linked waves to enemy spawner!
I was puzzled until… I realized that I had linked them by writing “2” into the size and then using the buttons on the side of the elements to select wave 1 and 2 from the list that appears.
I decided to try and do it like it was done in the lesson, by dragging wave 1 and 2 from the project section at the bottom side of unity, and it worked.
I admit that I don’t know why that made a difference, though, hopefully someone else can shed some light on this :grinning:

Privacy & Terms