ArgumentOutOfRangeException: Index was out of range. please help!

Hello, I have a problem with the code from the laser defender section of the unity 2d course
when calling the GetWayPoints() method from a scriptable object from the following code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "WaveConfig")]
public class WaveConfig : ScriptableObject
{
    [SerializeField] GameObject enemyPrefab;
    [SerializeField] GameObject pathPrefab;
    [SerializeField] float spawnTimer;
    [SerializeField] float randomness;
    [SerializeField] int enemyNumber;
    [SerializeField] float moveSpeed;
    private void Start()
    {
        
    }
    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;
    }

I call it in another script in the start method.

public class EnemyPathing : MonoBehaviour
{
    WaveConfig waveConfig;
    [SerializeField] List<Transform> wayPoints;
    [SerializeField] float moveSpeed;
    [SerializeField] int wayPointIndex = 0;
    private void Start()
    {
        transform.position = wayPoints[wayPointIndex].transform.position;
        wayPoints = waveConfig.GetWayPoints();
    }
    private void Update()
    {
        Move();
    }

    private void Move()
    {
        if (wayPointIndex <= wayPoints.Count - 1)
        {
            var targetPos = wayPoints[wayPointIndex].transform.position;
            var moveFrameBalance = moveSpeed * Time.deltaTime;
            transform.position = Vector2.MoveTowards(transform.position, targetPos, moveFrameBalance);
            if (transform.position == wayPoints[wayPointIndex].transform.position)
            {
                wayPointIndex++;
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

But it gives me the error “ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index”
The method is supposed to return a list.
So when i tried to print the array using Debug.Log(waveConfig.GetWayPoints()); without calling the function anywhere else, it still gives me the error and does not print anything.
I think it is a problem within the method itself.
Please help !

Hi Anwin,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

To which line in your code does the error message refer?

yes i have compared. I just tried to print the array waveWayPoints in the following method using
Debug.Log(waveWayPoints); but it prints “null”. I think the method is not returning any value.

   public List<Transform> GetWayPoints()
    {
        var waveWayPoints = new List<Transform>();
        foreach (Transform child in pathPrefab.transform)
        {
            waveWayPoints.Add(child);
        }
        return waveWayPoints;
    }

and I did assign all the values in the inspector.

Strange. Actually, it should return a List object, not null. Where exactly did you put the Debug.Log?

in the update method of a new object i created with the same script and changed the scriptableObject to mono behaviour.
to test if it would work.

waveWayPoints is a local variable inside the GetWayPoint method. You cannot access it from another method. Or do you mean that you assigned the returned object to another variable and logged that variable into your console? Without knowing exactly what you did, it is impossible for me to interpret the output correctly.

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

Privacy & Terms