I am getting this error. I am completely lost at the moment and can’t get further. I took your code from GITHUB and same issue.
My error Message:
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)
EnemiesSpawner+d__3.MoveNext () (at Assets/Script/EnemiesSpawner.cs:22)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
EnemiesSpawner:Start() (at Assets/Script/EnemiesSpawner.cs:15)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemiesSpawner : MonoBehaviour
{
[SerializeField] List<WavesConfig> waveConfigs;
int startingWave = 0;
// Use this for initialization
void Start()
{
var currentWave = waveConfigs[startingWave];
StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}
private IEnumerator SpawnAllEnemiesInWave(WavesConfig 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());
}
}
}
WavesConfig.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Enemy Wave Config")]
public class WavesConfig : ScriptableObject
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweekSpawns = 0.5f;
[SerializeField] float spawnRandomFactor = 0.3f;
[SerializeField] int numberOfEnemies = 5;
[SerializeField] float moveSpeed = 2f;
public GameObject GetEnemyPrefab(){ return enemyPrefab;}
public List<Transform> GetWaypoints()
{
var waveWaypoints = new List<Transform>();
foreach(Transform child in pathPrefab.transform)
{
waveWaypoints.Add(child);
}
return waveWaypoints;
}
public float GetTimeBetweenSpawns() { return timeBetweekSpawns; }
public float GetSpawnRandomFactor() { return spawnRandomFactor; }
public int GetNumberOfEnemies() { return numberOfEnemies; }
public float GetMoveSpeed() { return moveSpeed; }
}