Help Error "ArgumentOutOfRangeException: Index was out of range. "

I need help I’m fairly new to unity and im following a tutorial but they dont seem to have this problem on their side:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.
MapGenerator.MoveDown() at (MapGenerator.cs:60)
MapGenerator.GenerateMap() at (MapGenerator.cs:103)
MapGenerator.Start() at(MapGenerator.cs:27)

This is my code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class MapGenerator : MonoBehaviour
{
    public GameObject tile;

    [SerializeField]private int mapWidth;
    [SerializeField]private int mapHeight;

    private List<GameObject> mapTiles = new List<GameObject>();
    private List<GameObject> pathTiles = new List<GameObject>();
   
    private bool reachedX = false;
    private bool reachedY = false;
   
    private GameObject currentTile;
    private int currentIndex;
    private int nextIdx;

    public Color pathColor;
    private void Start()
    {
        GenerateMap();
    }

    private List<GameObject> getTopEdgeTiles()
    {
        List<GameObject> edgeTiles = new List<GameObject>();

        for (int i = mapWidth * (mapHeight - 1); i < mapWidth * mapHeight; i++)
        {
            edgeTiles.Add(mapTiles[i]);
        }
       
        return edgeTiles;
    }

    private List<GameObject> getBottomEdgeTiles()
    {
        List<GameObject> edgeTiles = new List<GameObject>();
       
        for(int j = 0; j < mapWidth; j++)
        {
            edgeTiles.Add(mapTiles[j]);
        }
       
        return edgeTiles;
    }

    private void MoveDown()
    {
        pathTiles.Add(currentTile);
        currentIndex = mapTiles.IndexOf(currentTile);
        nextIdx = currentIndex - mapWidth;
        currentTile = mapTiles[nextIdx];
    }

    private void MoveLeft()
    {
        pathTiles.Add(currentTile);
        currentIndex = mapTiles.IndexOf(currentTile);
        nextIdx = currentIndex - 1;
        currentTile = mapTiles[nextIdx];
    }

    private void MoveRight()
    {
        pathTiles.Add(currentTile);
        currentIndex = mapTiles.IndexOf(currentTile);
        nextIdx = currentIndex + 1;
        currentTile = mapTiles[nextIdx];
    }
    private void GenerateMap()
    {
        for (int y = 0; y < mapHeight; y++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                GameObject newTile = Instantiate(tile);
                mapTiles.Add(tile);

                newTile.transform.position = new Vector2(x, y);
            }
        }

        List<GameObject> topEdgeTiles = getTopEdgeTiles();
        List<GameObject> bottomEdgeTiles = getBottomEdgeTiles();
       
        GameObject startTile;
        GameObject endTile;

        int rand1 = Random.Range(0, mapWidth);
        int rand2 = Random.Range(0, mapWidth);

        startTile = topEdgeTiles[rand1];
        endTile = bottomEdgeTiles[rand2];

        currentTile = startTile;
        MoveDown();
       
        int loopcount = 0;
        while (!reachedX)
        {
            loopcount++;
            if (loopcount > 100)
            {
                break;
            }
            if (currentTile.transform.position.x > endTile.transform.position.x)
            {
                MoveLeft();
            }
            else if(currentTile.transform.position.x < endTile.transform.position.x)
            {
                MoveRight();
            }
            else
            {
                reachedX = true;
            }
        }

        while (!reachedY)
        {
            if (currentTile.transform.position.y > endTile.transform.position.y)
            {
                MoveDown();
            }
            else
            {
                reachedY = true;
            }
        }
        pathTiles.Add(endTile);
        foreach (GameObject obj in pathTiles)
        {
            obj.GetComponent<SpriteRenderer>().color = pathColor;
        }
    }
}

Sounds like you’ll need to go back to the tutorial and follow it again.
Because whatever value nextIdx is, it’s either < 0 or > the amount of items in your mapTiles list.

You can add some debug.log statements to get those values easily and help make sure you’re within the proper range of your list.