Index was out of range Must be non-negative

Help me please, i have a error that says:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Here is my codes

WaveConfig

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

[CreateAssetMenu(menuName = “Enemy Wave Config”)]
public class WaveConfig : ScriptableObject
{
[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 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 timeBetweenSpawns; }

public float GetSpawnRandomFactor() { return spawnRandomFactor; }

public int GetNumberOfEnemies() { return numberOfEnemies; }

public float GetmoveSpeed() { return moveSpeed; }

}

Enemy Spawner

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

public class EnemySpawner : MonoBehaviour
{

[SerializeField] List<WaveConfig> 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)
{
Instantiate(
waveConfig.GetEnemyPrefab(),
waveConfig.GetWaypoints()[0].transform.position,
Quaternion.identity);
yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
}
}

Enemy Pathing

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

public class EnemyPathing : MonoBehaviour
{
[SerializeField] WaveConfig waveConfig;
[SerializeField] List waypoints;
[SerializeField] float moveSpeed = 2f;
int waypointIndex = 0;

// Start is called before the first frame update
void Start()
{
    waypoints = waveConfig.GetWaypoints();
    transform.position = waypoints[waypointIndex].transform.position;
}

// Update is called once per frame
void Update()
{
    Move();
}

private void Move()
{
    if (waypointIndex <= waypoints.Count - 1)
    {
        var targetPosition = waypoints[waypointIndex].transform.position;
        var movementThisFrame = moveSpeed * Time.deltaTime;
        transform.position = Vector2.MoveTowards
            (transform.position, targetPosition, movementThisFrame);

        if (transform.position == targetPosition)
        {
            waypointIndex++;
        }
    }
    else
    {
        Destroy(gameObject);
    }
}

}

If you double click the error it should take you to the offending line.

It has to do with accessing an element in an array. Most likely you either have not checked the index to be in the bounds of the array or you have not instantiated the elements in the array.

transform.position = waypoints[waypointIndex].transform.position;

waveConfig.GetWaypoints()[0]

waypointIndex may be larger then the array or there may be no waypoints so even index 0 is larger then an empty array.

2 Likes

I don’t quite understand i am 9 years old.
Which script can i solve the error?

Not sure if it’ll help, but I was having this issue and it turned out that my Enemy Spawner Game Object didn’t save the waves when I dragged them to the attached script.

Here’s the wrong line, you have to change the “[SerializeField] waypoints” into “Listwaypoints”

whole sentence is like this:

[SerializeField] WaveConfig waveConfig;
List<Transform> waypoints;
[SerializeField] float moveSpeed = 2f;
int waypointIndex = 0;

I face the same problem too (it was really frustrated),
and spend 1 hr to watch the chapter from the beginning,
finally find the bug!

Privacy & Terms