I got this problem indexOutOfRangeException: Index was outside the bounds of the array

I want to make a memory game and I ran into this issue when trying to make a randomizer script

indexOutOfRangeException: Index was outside the bounds of the array.
GameController.Randomiser (System.Int32[] locations) (at Assets/Scripts/GameControllerScript.cs:22)
GameController.Start () (at Assets/Scripts/GameControllerScript.cs:35)

this is the script what I think made the problem

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

public class GameController : MonoBehaviour
{
    public const int columns = 4;
    public const int rows = 2;

    public const float xspace = 4f;
    public const float yspace = -5f;


    [SerializeField] private MainImageScript startObject;
    [SerializeField] private Sprite[] images;

    private int[] Randomiser(int[] locations)
    {
        int[] array = locations.Clone() as int[];
        for(int i=0; 1< array.Length; i++)
        {
            int newArray = array[i];
            int j = Random.Range(i, array.Length);
            array[i] = array[j];
            array[j] = newArray;
            

        }
        return array;
    }

    private void Start()
    {
        int[] locations = { 0, 0, 1, 1, 2, 2, 3, 3, };
        locations = Randomiser(locations);

        Vector3 startPosition = startObject.transform.position;

        for(int i = 0; i < columns; i++)
        {
            for(int j = 0; j < rows; j++)
            {
                MainImageScript gameImage;
                if(i == 0 && j == 0)
                {
                    gameImage = startObject;
                }
                else
                {
                    gameImage = Instantiate(startObject) as MainImageScript; 
                }
                
                int index = j * columns + i;
                int id = locations[index];
                gameImage.ChangeSprite(id, images[id]);

                float positionX = (xspace * i) + startPosition.x;
                float positionY = (yspace * j) + startPosition.y;

                gameImage.transform.position = new Vector3(positionX, positionY, startPosition.z);
            }
        }
    }

}
    

thank you so much if you want to help I am a beginner so if you want to modify this script I would like it so much!

1 Like

The for loop in your Randomiser function looks like this: for(int i=0; 1< array.Length; i++).
But instead should be for(int i=0; i < array.Length; i++).